/**
 * Created by sunyxq on 16/3/26.
 */
angular.module('hyt.manageYunDanCtrl',[])

    .controller('CydListCtrl',function($scope,CydListFactory,$state,ENV,Storage,$stateParams,$ionicHistory,$filter,CydChangeFactory,$cordovaHdoitBle,$ionicLoading,$timeout,$ionicPopup,AddCydFactory,PrinterFactory){
        $scope.showloading=true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var dw_id=Storage.get('dw_id');
        var fwqbs=ENV.fwqbs;
        var zsxm=Storage.get('zsxm');
        var url=Storage.get('url');
        $scope.bmmc=Storage.get('bmmc');
        Storage.set('scrollChangeWz',null);
        Storage.set('cydData',null);
        Storage.set('fkfs',null);
        Storage.set('shfs',null);
        $scope.myGoBack = function() {
            $state.go("menu");
        };

        CydListFactory.getCydList(sid,user_id,yh_id,zd_id,dw_id,$stateParams.cydh,$stateParams.kssj,$stateParams.jssj,$stateParams.fhrmc,$stateParams.fhrdh,$stateParams.shrdh,$stateParams.cyzt,$stateParams.shbm);
        CydListFactory.getCydhj(sid,user_id,yh_id,zd_id,dw_id,$stateParams.cydh,$stateParams.kssj,$stateParams.jssj,$stateParams.fhrmc,$stateParams.fhrdh,$stateParams.shrdh,$stateParams.cyzt,$stateParams.shbm);

        $scope.$on('CydList.cydUpdated', function() {
            var dataFactory=CydListFactory.getCyd();
            console.log(dataFactory.cyzt);
            $scope.cydData=dataFactory.data;
            console.log(dataFactory.data);
            $scope.showloading=false;

        });
        $scope.$on('CydHj.cydUpdated',function(){
            var cydHj=CydListFactory.getResult();
            $scope.cydhj=cydHj;
        });
        $scope.ddzdh;
        //下拉更新
        $scope.doRefresh=function(){
            CydListFactory.getCydList(sid,user_id,yh_id,zd_id,dw_id,$stateParams.cydh,$stateParams.kssj,$stateParams.jssj,$stateParams.fhrmc,$stateParams.fhrdh,$stateParams.shrdh,$stateParams.cyzt,$stateParams.shbm);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉更新
        $scope.loadMore=function(){
            CydListFactory.getMoreCyd(sid,user_id,yh_id,zd_id,dw_id,$stateParams.cydh,$stateParams.kssj,$stateParams.jssj,$stateParams.fhrmc,$stateParams.fhrdh,$stateParams.shrdh,$stateParams.cyzt,$stateParams.shbm);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        //下一页
        $scope.hasNextPage = function() {
            return CydListFactory.hasNextPage();
        };

        $scope.dragRight=function(){
            $state.go('menu');
        };
        var dyfs,hour,today,time,shtype;

        $scope.dy={
            sfdywlgscg:'',
            sfdyfhrcg:'',
            sfdyhwbt:'',
            hwbtdyfs:'',
            hwdysl:'',
            fhrcgsl:1,
            wlgscgsl:1,
            read:false,
            qsh:1,
            sfdyxxf:''
        };
        //初始化打印方式
        function initDyfs(){
            CydChangeFactory.fetchPrintYd(sid,user_id,yh_id,zd_id);
            $scope.$on('QueryYd.PrintSet',function(){
                var set=CydChangeFactory.getSetYd();
                if(set.ACTION_RETURN_CODE=='000000'){
                    var list=set.ACTION_INFO;
                    if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='1'){
                        dyfs='1'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='1'){
                        dyfs='2'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='1'){
                        dyfs='3'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='0'){
                        dyfs='4'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='0'){
                        dyfs='5'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='1'){
                        dyfs='6'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='0'){
                        dyfs='7'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='0'){
                        dyfs='8'
                    }
                    $scope.dy.dyxw=list.hwbtdyxw;
                    $scope.dy.sfdyfhrcg=list.sfdyfhrcg;
                    $scope.dy.sfdywlgscg=list.sfdywlgscg;
                    $scope.dy.sfdyhwbt=list.sfdyhwbt;
                    $scope.dy.hwbtdyfs=list.hwbtdyxw;
                    $scope.title=list.gsmc;
                    $scope.zrtw=list.bz;
                    $scope.kfdh=list.kfdh;
                    $scope.dy.sfdyxxf=list.sfdyxxf;
                }
            });
        }

        function readyToPrintGscg(){
            var site='';
            var district="";
            var sjdh='';
            var ds='代收:';
            var dsje=$scope.cyd.dsk_hf+'元';
            var dfje=$scope.cyd.dsk_xf+'元';
            var daofje=$scope.cyd.yf_tf+'元';
            var xxf_xfje=$scope.cyd.xxf_xf+'元';
            var xxf_qfje=$scope.cyd.xxf_qf+'元';
            var hwsl=$scope.cyd.sl+'件';
            var hwmc=$scope.cyd.hwmc;
            var yfdfje=$scope.cyd.yf_df+'元';
            var yf_hf,yf_xf,yf_tf,yf_yj,yfdk,sjthf,shf1,yf_df,bf;
            yf_hf=(!isNaN($scope.cyd.yf_hf))?$scope.cyd.yf_hf:0;
            yf_xf=(!isNaN($scope.cyd.yf_xf))?$scope.cyd.yf_xf:0;
            yf_tf=(!isNaN($scope.cyd.yf_tf))?$scope.cyd.yf_tf:0;
            yf_yj=(!isNaN($scope.cyd.yf_yj))?$scope.cyd.yf_yj:0;
            yfdk=(!isNaN($scope.cyd.yf_ycdk))?$scope.cyd.yf_ycdk:0;
            yf_df=(!isNaN($scope.cyd.yf_df))?$scope.cyd.yf_df:0;
            shf1=(!isNaN($scope.cyd.shf1))?$scope.cyd.shf1:0;
            sjthf=(!isNaN($scope.cyd.thf))?$scope.cyd.thf:0;
            bf=(!isNaN($scope.cyd.bf))?$scope.cyd.bf:0;

            var yuan='元';
            if(yf_hf==''||yf_hf==null||yf_hf==undefined){
                yf_hf=0;
            }
            if(yf_xf==''||yf_xf==null||yf_xf==undefined){
                yf_xf=0;
            }
            if(yf_tf==''||yf_tf==null||yf_tf==undefined){
                yf_tf=0;
            }
            if(yf_yj==''||yf_yj==null||yf_yj==undefined){
                yf_yj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined){
                yfdk=0;
            }
            if(bf==''||bf==null||bf==undefined){
                bf=0;
            }
            if(shf1==''||shf1==null||shf1==undefined){
                shf1=0;
            }
            if(yf_df==''||yf_df==null||yf_df==undefined){
                yf_df=0;
            }
            if(sjthf==''||sjthf==null||sjthf==undefined){
                sjthf=0;
            }
            var yfje=(parseFloat(yf_hf)+parseFloat(yf_xf)+parseFloat(yf_tf)+parseFloat(yf_yj)+parseFloat(yfdk)+parseFloat(shf1)+parseFloat(sjthf)+parseFloat(bf)-parseFloat(yf_df)).toFixed(2);

            if(yfje==0||yfje==''||yfje==null){
                yfje=0;
            }


            var shrmc=$scope.cyd.shrmc+'';
            var fhrmc=$scope.cyd.fhrmc+'';
            var shrdh=$scope.cyd.shrdh+'';
            var fhrdh=$scope.cyd.fhrdh+'';
            var df='代收垫付:';
            var dou1=',';
            var dou2=',';
            var dou3=',';
            var dou4=',';
            var yf='运费:';
            var daof='到付:';
            var yfdf='运费垫付:';
            var xxf_xf='现返:';
            var xxf_qf='欠返:';
            if($scope.cyd.mdd==""&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc;
                district=$scope.cyd.shbmmc;
                sjdh=$scope.ddzdh;
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
                district=$scope.cyd.mdd;
                sjdh='';
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc+" → "+$scope.cyd.mdd;
                district=$scope.cyd.shbmmc;
                sjdh=$scope.ddzdh;
            }
            if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf==''){
                dsje='0元';
                dfje='';
                dou1='';
                df='';
            }else if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf!=''){
                dsje='0元';
                dfje=$scope.cyd.dsk_xf+'元';
                dou1=',';
            }else if($scope.cyd.dsk_hf!=''&&$scope.cyd.dsk_xf==''){
                dou1='';
                df='';
                dfje='';
            }
            if($scope.cyd.yf_hf+$scope.cyd.yf_tf+$scope.cyd.yf_ycdk+$scope.cyd.yf_xf+$scope.cyd.yf_yj==''){
                yfje='0'
            }
            if ($scope.dy.sfdyxxf==0){
              xxf_xf='';
              xxf_xfje='';
              xxf_qf='';
              xxf_qfje='';
              dou2='';
            }
            if($scope.cyd.xxf_xf==''){
              xxf_xf='';
              xxf_xfje='';
            }
            if ($scope.cyd.xxf_qf==''){
              xxf_qf='';
              xxf_qfje='';
              dou2='';
            }
            if($scope.cyd.yf_df==''){
                dou3='';
                yfdf='';
                yfdfje='';
            }
            if($scope.cyd.yf_tf==''){
                daofje='0元';
            }
            var dou5=',';
            var hdfs='回单:';
            var qdfs=$scope.cyd.qdfs+'份';
            if($scope.cyd.qdfs==''){
                dou5='';
                hdfs='';
                qdfs='';
            }
            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":430};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var yundanCode={"x":10,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("运单号：",yundanCode);

            //运单号
            var yundanCode1={"x":110,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);

            var date={"x":320,"y":65,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(today,date);

            //var second={"x":460,"y":17.5,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("第二联",second);
            var kefuPhone={"x":420,"y":17.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);

            var wlgslc={"x":490,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("存根",wlgslc);
            //始发站
            var route1={"x":10,"y":105,"textSize":15,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site+'  '+hwmc+hwsl,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":90,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);

            //送货方式
            var shfs={"x":380,"y":145,"textSize":3,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shtype,shfs);

            //收货人
            var shf={"x":10,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("收货方",shf);

            //收货人名称
            var recieveP={"x":110,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrmc,recieveP);

            //收货人电话
            var shPhone={"x":210,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrdh,shPhone);

            //发货人
            var fhf={"x":10,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方",fhf);

            //发货人名称
            var sendP={"x":110,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrmc,sendP);

            //发货人电话
            var fhPhone={"x":210,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrdh,fhPhone);

            //代收、代收垫付
            var dsdf={"x":10,"y":215,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(ds+dsje+dou1+df+dfje,dsdf);

            //运费、到付、运费垫付
            var yfdfdf={"x":10,"y":245,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yf+yfje+yuan+dou2+daof+daofje+dou5+hdfs+qdfs,yfdfdf);

            //声明价值
            var note={"x":10,"y":285,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yfdf+yfdfje+dou3+xxf_xf+ xxf_xfje+dou2+xxf_qf+xxf_qfje,note);

            //var kefuPhone={"x":395,"y":245,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("电话："+$scope.kfdh,kefuPhone);
            //到站信息
            var mddNote={"x":10,"y":315,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(district+' '+sjdh,mddNote);

            //备注
            var warn1={"x":10,"y":345,"width":520,"height":64,"fontSize":2,"rotate":0,"bold":1,"underline":false,"reverse":false};
            $cordovaHdoitBle.multilineText($scope.zrtw,warn1);

            //开票人
            var kpr={"x":380,"y":315,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("开票人："+zsxm,kpr);
            //扫码跟踪
            var xyt={"x":470,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("扫码跟踪",xyt);
            //打印二维码
            var textOption2 = {"x":460,"y":90,"size":3};
            $cordovaHdoitBle.printQRcodeZhike(url+"/jsp/scanning.jsp?cyd_id="+$scope.cyd.cyd_id+"&bs="+fwqbs,textOption2);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        function readyToPrintFhrcg(){
            var site='';
          var district="";
          var sjdh='';
            var ds='代收:';
            var dsje=$scope.cyd.dsk_hf+'元';
            var dfje=$scope.cyd.dsk_xf+'元';
            var daofje=$scope.cyd.yf_tf+'元';
            var xxf_xfje=$scope.cyd.xxf_xf+'元';
            var xxf_qfje=$scope.cyd.xxf_qf+'元';
            var hwsl=$scope.cyd.sl+'件';
            var hwmc=$scope.cyd.hwmc;
            var yfdfje=$scope.cyd.yf_df+'元';
            var yf_hf,yf_xf,yf_tf,yf_yj,yfdk,sjthf,shf1,yf_df,bf;
            yf_hf=(!isNaN($scope.cyd.yf_hf))?$scope.cyd.yf_hf:0;
            yf_xf=(!isNaN($scope.cyd.yf_xf))?$scope.cyd.yf_xf:0;
            yf_tf=(!isNaN($scope.cyd.yf_tf))?$scope.cyd.yf_tf:0;
            yf_yj=(!isNaN($scope.cyd.yf_yj))?$scope.cyd.yf_yj:0;
            yfdk=(!isNaN($scope.cyd.yf_ycdk))?$scope.cyd.yf_ycdk:0;
            yf_df=(!isNaN($scope.cyd.yf_df))?$scope.cyd.yf_df:0;
            shf1=(!isNaN($scope.cyd.shf1))?$scope.cyd.shf1:0;
            sjthf=(!isNaN($scope.cyd.thf))?$scope.cyd.thf:0;
            bf=(!isNaN($scope.cyd.bf))?$scope.cyd.bf:0;

            var yuan='元';
            if(yf_hf==''||yf_hf==null||yf_hf==undefined){
                yf_hf=0;
            }
            if(yf_xf==''||yf_xf==null||yf_xf==undefined){
                yf_xf=0;
            }
            if(yf_tf==''||yf_tf==null||yf_tf==undefined){
                yf_tf=0;
            }
            if(yf_yj==''||yf_yj==null||yf_yj==undefined){
                yf_yj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined){
                yfdk=0;
            }
            if(bf==''||bf==null||bf==undefined){
                bf=0;
            }
            if(shf1==''||shf1==null||shf1==undefined){
                shf1=0;
            }
            if(yf_df==''||yf_df==null||yf_df==undefined){
                yf_df=0;
            }
            if(sjthf==''||sjthf==null||sjthf==undefined){
                sjthf=0;
            }
            var yfje=(parseFloat(yf_hf)+parseFloat(yf_xf)+parseFloat(yf_tf)+parseFloat(yf_yj)+parseFloat(yfdk)+parseFloat(shf1)+parseFloat(sjthf)+parseFloat(bf)-parseFloat(yf_df)).toFixed(2);

            if(yfje==0||yfje==''||yfje==null){
                yuan='元';
            }
            var shrmc=$scope.cyd.shrmc+'';
            var fhrmc=$scope.cyd.fhrmc+'';
            var shrdh=$scope.cyd.shrdh+'';
            var fhrdh=$scope.cyd.fhrdh+'';
            var df='代收垫付:';
            var dou1=',';
            var dou2=',';
            var dou3=',';
            var dou4=',';
            var yf='运费:';
            var daof='到付:';
            var yfdf='运费垫付:';
            var xxf_xf='现返:';
            var xxf_qf='欠返:';
            if($scope.cyd.mdd==""&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc;
              district=$scope.cyd.shbmmc;
              sjdh=$scope.ddzdh;
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
              district=$scope.cyd.mdd;
              sjdh='';
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc+" → "+$scope.cyd.mdd;
              district=$scope.cyd.shbmmc;
              sjdh=$scope.ddzdh;
            }
            if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf==''){
                dsje='0元';
                dfje='';
                dou1='';
                df='';
            }else if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf!=''){
                dsje='0元';
                dfje=$scope.cyd.dsk_xf+'元';
                dou1=',';
            }else if($scope.cyd.dsk_hf!=''&&$scope.cyd.dsk_xf==''){
                dou1='';
                df='';
                dfje='';
            }
            if ($scope.dy.sfdyxxf==0){
              xxf_xf='';
              xxf_xfje='';
              xxf_qf='';
              xxf_qfje='';
              dou2='';
            }
            if($scope.cyd.xxf_xf==''){
              xxf_xf='';
              xxf_xfje='';
            }
            if ($scope.cyd.xxf_qf==''){
              xxf_qf='';
              xxf_qfje='';
              dou2='';
            }
            if($scope.cyd.yf_df==''){
                dou3='';
                yfdf='';
                yfdfje='';
            }
            if($scope.cyd.yf_tf==''){
                daofje='0元';
            }
            //签回单
            var dou5=',';
            var hdfs='回单:';
            var qdfs=$scope.cyd.qdfs+'份';
            if($scope.cyd.qdfs==''){
                dou5='';
                hdfs='';
                qdfs='';
            }
            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":400};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var yundanCode={"x":10,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("运单号：",yundanCode);

            //运单号
            var yundanCode1={"x":110,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);

            //var first={"x":460,"y":17.5,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("第一联",first);

            var date={"x":320,"y":65,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(today,date);

            var fhrlc={"x":460,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方留存",fhrlc);
            //始发站
            var route1={"x":10,"y":105,"textSize":15,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site+'  '+hwmc+hwsl,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":90,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);

            //送货方式
            var shfs={"x":380,"y":145,"textSize":3,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shtype,shfs);

            //收货人
            var shf={"x":10,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("收货方",shf);

            //收货人名称
            var recieveP={"x":110,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrmc,recieveP);

            //收货人电话
            var shPhone={"x":210,"y":145,"textSize":6,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrdh,shPhone);
            //发货人
            var fhf={"x":10,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方",fhf);

            //发货人名称
            var sendP={"x":110,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrmc,sendP);

            //发货人电话
            var fhPhone={"x":210,"y":175,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrdh,fhPhone);

            //代收、代收垫付
            var dsdf={"x":10,"y":215,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(ds+dsje+dou1+df+dfje,dsdf);

            //运费、到付、运费垫付
            var yfdfdf={"x":10,"y":245,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yf+yfje+yuan+dou2+daof+daofje+dou5+hdfs+qdfs,yfdfdf);

            //货物名称件数声明价值
            var note={"x":10,"y":285,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yfdf+yfdfje+dou3+xxf_xf+ xxf_xfje+dou2+xxf_qf+xxf_qfje,note);

            var kefuPhone={"x":420,"y":17.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);
            //到站信息
            var mddNote={"x":10,"y":315,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(district+" "+sjdh,mddNote);

            //备注
            var warn1={"x":10,"y":345,"width":520,"height":64,"fontSize":2,"rotate":0,"bold":1,"underline":false,"reverse":false};
            $cordovaHdoitBle.multilineText($scope.zrtw,warn1);

            //开票人
            var kpr={"x":400,"y":315,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("开票人："+zsxm,kpr);
            //关注信运通
            //var xyt={"x":440,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("关注信运通",xyt);
            //扫码跟踪
            var xyt={"x":470,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("扫码跟踪",xyt);
            //打印二维码
            var textOption2 = {"x":460,"y":90,"size":3};
            $cordovaHdoitBle.printQRcodeZhike(url+"/jsp/scanning.jsp?cyd_id="+$scope.cyd.cyd_id+"&bs="+fwqbs,textOption2);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        function readyToPrintHwbq(hwbh){
            var site='';
            if($scope.cyd.mdd==""&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc;
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
            }else if($scope.cyd.mdd!=''&&$scope.cyd.shbmmc!=''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.shbmmc+" → "+$scope.cyd.mdd;
            }

            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":430};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":-5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var kefuPhone={"x":420,"y":7.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);

            //运单号
            var yundanCode1={"x":10,"y":80,"textSize":20,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);
            //始发站
            var route1={"x":10,"y":160,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":165,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);
            //货名、件数、收货人姓名
            var hm = {"x":10,"y":235,"textSize":15,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.hwmc+$scope.cyd.sl+'件，第'+hwbh+"件   "+$scope.cyd.shrmc,hm);

            //打印一维码

            var textOption3 = {"x":10,"y":280,"type":'128','width':1,'height':85,'rotate':false};
            $cordovaHdoitBle.printBarcodeZhike($scope.cyd.cydh,textOption3);

            var hwxx={"x":10,"y":380,"textSize":1,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh+'-'+hwbh+'  '+time,hwxx);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        //不足n位前补0
        function padLeft(str, lenght) {
            if (str.length >= lenght)
                return str;
            else
                return padLeft("0" + str, lenght);
        }
        //打印
        initDyfs();
        var hwbh='';
        var isConeccted;
        function getBleName(){
            //蓝牙设备名称
            $cordovaHdoitBle.bluetoothName(function(success){
                if(success!=null){
                    isConeccted='1';
                }
                else {
                    isConeccted='0';
                }
            },function(err){
                isConeccted='0';
            });
        }
        window.setInterval(getBleName,1);
        $scope.yundandy=function(id){
            CydChangeFactory.getChangeDetail(id,sid,user_id,yh_id);
            $scope.$on('CydDetail.changeUpdated',function(){
                var cydChangeData = CydChangeFactory.getChangeDetailContent();
                $scope.cyd=cydChangeData;
                console.log(cydChangeData);
            });
            if($scope.dy.sfdyhwbt=='1'&&$scope.dy.hwbtdyfs=='1'){
                $scope.dy.hwdysl=$scope.cyd.sl;
                $scope.dy.read=true;
            }else if($scope.dy.sfdyhwbt=='0'){
                $scope.dy.hwdysl=0;
                $scope.dy.read=true;
            }else {
                $scope.dy.read=false;
                $scope.dy.hwdysl='';
            }
            hour=$filter('date')(new Date(),'HH:mm:ss');
            today=$filter('date')($scope.cyd.cyrq,'yyyy.MM.dd');
            time=$filter('date')($scope.cyd.cyrq+" "+hour,'yyyy-MM-dd HH:mm:ss');
            var ddz_id=$scope.cyd.shbm_id;
            $timeout(function(){
                AddCydFactory.getDdzPhone1(sid,user_id,ddz_id,yh_id);
                $scope.$on('getPhone1.ddz',function(){
                    var result=AddCydFactory.getDdzPhoneResult1();
                    if(result.ACTION_RETURN_CODE=='000000'){
                        $scope.ddzdh=result.ACTION_INFO.RESULT_LIST[0].lxdh;
                    }
                });
            },160);
            switch ($scope.cyd.shfs){
                case 1 :
                    shtype = '自提';
                    break;
                case 2 :
                    shtype = '送货';
                    break;
                case 3 :
                    shtype = '上楼';
                    break;
                default :
                    break;
            }
            if(isConeccted=='0'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请先连接打印机", duration: 1500
                });
                $state.go('addPrinter');
            }else{
                var confirmPopup = $ionicPopup.confirm({
                    title:'打印确认',
                    scope:$scope,
                    template: '<div class="list">' +
                    '<div class="item" ng-if="dy.sfdyfhrcg==1">' +
                    '<div class="row" style="font-size: 16px" >' +
                    '<div class="col-60">发货人存根：</div> ' +
                    '<input type="number" id="fhrcgsl" class="col-25" ng-model="dy.fhrcgsl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                    '<div  class="col-15">份</div>' +
                    '</div>' +
                    '</div>' +
                    '<div class="item" ng-show="dy.sfdywlgscg==1">' +
                    '<div class="row" style="font-size: 16px" >' +
                    '<div class="col-60">物流公司存根：</div>' +
                    '<input type="number" id="wlgscgsl" class="col-25" ng-model="dy.wlgscgsl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                    '<div class="col-15">份</div>' +
                    '</div>' +
                    '</div>'+
                    '<div class="item" style="height: 55px"><div class="row" style="font-size: 16px">' +
                    '<div class="col-60">货物标贴份数：</div>' +
                    '<input type="number" id="hwdysl" class="col-25" ng-model="dy.hwdysl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                    '<div class="col-15">份</div>'+
                    '</div>' +
                    '</div>' +
                    '<div class="item" ng-if="dy.sfdyhwbt==1">' +
                    '<div class="row" style="font-size: 16px">' +
                    '<div class="col-70">货贴打印起始号：</div>' +
                    '<input type="number" class="col-20" ng-model="dy.qsh" maxlength="20"  style="text-align: right;margin-top: -7px;margin-right: 17px">' +
                    '</div>' +
                    '</div>'+
                    '</div>',
                    okText:'确定',
                    cancelText:'取消'
                });
                confirmPopup.then(function(res) {
                    if (res) {
                        if($scope.dy.qsh==0){
                            $scope.dy.qsh=1;
                        }
                        switch (dyfs){
                            case '1':
                                for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                  readyToPrintFhrcg();
                                }
                                for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                  readyToPrintGscg();
                                }
                                for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++) {
                                    //$scope.dy.qsh=i;
                                    hwbh=padLeft(i, 3);
                                    readyToPrintHwbq(hwbh);
                                }
                                break;
                            case '2':
                                for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                  readyToPrintGscg();
                                }
                                for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                    //$scope.dy.qsh=i;
                                    hwbh=padLeft(i, 3);
                                    readyToPrintHwbq(hwbh);
                                }
                                break;
                            case '3':
                                for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                  readyToPrintGscg();
                                }
                                break;
                            case '4':
                                break;
                            case '5':
                                for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                    //$scope.dy.qsh=i;
                                    hwbh=padLeft(i, 3);
                                    readyToPrintHwbq(hwbh);
                                }
                                break;
                            case '6':
                                for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                  readyToPrintFhrcg();
                                }
                                for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                  readyToPrintGscg();
                                }
                                break;
                            case '7':
                                for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                  readyToPrintFhrcg();
                                }
                                break;
                            case '8':
                                for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                  readyToPrintFhrcg();
                                }
                                for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                    //$scope.dy.qsh=i;
                                    hwbh=padLeft(i, 3);
                                    readyToPrintHwbq(hwbh);
                                }
                                break;
                        }
                        $state.go('cydlist');
                        //$ionicHistory.goBack();
                    } else {
                        $state.go('cydlist');
                        //$ionicHistory.goBack();
                    }
                });
                if($scope.dy.sfdyhwbt=='1'&&$scope.dy.hwbtdyfs!='1'){
                    $timeout(function(){
                        $('#hwdysl').focus();
                    },500);
                }
            }
        };

        //菜单搜索运单
        var re = /^[0-9]+.?[0-9]*$/;
        var mc,cydh,kssj,jssj,fhrdh,shrdh,cyzt,shbm_id;

        $scope.search={
            content:''
        };
        //$scope.cydhj1={
        //    ydhj:0,
        //    yfhj:0,
        //    hkhj:0
        //};
        $scope.keyCode;
        $scope.$on('$ionicView.enter', function() {
            if ($("#menusearch").val()==''){
                $("#menusearch").focus();
            }
        });
        document.onkeydown=function(event){
            if (!re.test($scope.search.content)){
                mc=$scope.search.content;
            }else {
                cydh=$scope.search.content;
            }
            $scope.keyCode=event.keyCode;
            if(event.keyCode==13){
                $scope.showloading1=true;
                if($scope.search.content!=''){
                    CydListFactory.getCydhjmenu(sid,user_id,yh_id,zd_id,dw_id,cydh,mc);
                    CydListFactory.getCydList1(sid,user_id,yh_id,zd_id,dw_id,cydh,kssj,jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
                }else {
                    CydListFactory.getCydhjmenu(sid,user_id,yh_id,'','',cydh,mc);
                    CydListFactory.getCydList1(sid,user_id,yh_id,'','','','',jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
                }
            }
        };
        //下拉更新
        $scope.doRefresh1=function(){
            if($scope.search.content!=''&&$scope.keyCode==13){
                CydListFactory.getCydhjmenu(sid,user_id,yh_id,zd_id,dw_id,cydh,mc);
                CydListFactory.getCydList1(sid,user_id,yh_id,zd_id,dw_id,cydh,kssj,jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
            }
            else {
                CydListFactory.getCydhjmenu(sid,user_id,yh_id,'','',cydh,mc);
                CydListFactory.getCydList1(sid,user_id,yh_id,'','','',kssj,jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
            }
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉更新
        $scope.loadMore1=function(){
            if($scope.search.content!=''){
                CydListFactory.getCydhjmenu(sid,user_id,yh_id,zd_id,dw_id,cydh,mc);
                CydListFactory.getMoreCyd1(sid,user_id,yh_id,zd_id,dw_id,cydh,kssj,jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
            }
            else{
                CydListFactory.getCydhjmenu(sid,user_id,yh_id,'','',cydh,mc);
                CydListFactory.getMoreCyd1(sid,user_id,yh_id,'','','',kssj,jssj,mc,fhrdh,shrdh,cyzt,shbm_id);
            }
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.$on('CydList1.cydUpdated', function() {
            var dataFactory=CydListFactory.getCyd1();
            console.log(dataFactory.cyzt);
            $scope.cydData1=dataFactory.data;
            console.log(dataFactory);
            $scope.showloading1=false;
        });
        $scope.$on('CydHjmenu.cydUpdated',function(){
            var cydHj=CydListFactory.getResultmenu();
            $scope.cydhj1=cydHj;
        });
        //下一页
        $scope.hasNextPage1 = function() {
            return CydListFactory.hasNextPage1();
        };
    })

    .controller('CydDetailCtrl',function($scope,$stateParams,CydDetailFactory,CydProcessFactory,$cordovaGeolocation,$ionicHistory,Storage,GPSFactory,$window,$ionicPopup){
        var id = $stateParams['cyd_id'];
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        CydDetailFactory.getDetail(id,sid,user_id,yh_id);
        //下拉更新
        $scope.doRefresh=function(){
            CydDetailFactory.getDetail(id,sid,user_id,yh_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.selectTab=function(){
            CydDetailFactory.getDetail(id,sid,user_id,yh_id);
            CydProcessFactory.getProcess(id,sid,user_id,yh_id);
        };
        $scope.$on('CydDetail.detailUpdated',function(){
           var cydDetailData= CydDetailFactory.getDetailContent();
            console.log(cydDetailData);
            switch (cydDetailData.shfs){
                case 1 :
                    $scope.shfs = '自提';
                    break;
                case 2 :
                    $scope.shfs = '送货';
                    break;
                case 3 :
                    $scope.shfs = '上楼';
                    break;
                default :
                    break;
            }

            switch (cydDetailData.cyzt) {
            case 1:
                $scope.cyzt = "待受理";
                break;
            case 2:
                $scope.cyzt = "已拒收";
                break;
            case 3:
                $scope.cyzt = "已承运";
                break;
            case 4:
                $scope.cyzt = "已装车";
                break;
            case 5:
                $scope.cyzt = "已签收";
                break;
            case 6:
                $scope.cyzt = "已撤销";
                break;
            case 10:
                $scope.cyzt = "承运中";
                break;
            case 20:
                $scope.cyzt = "已装车";
                break;
                case 30 :
                $scope.cyzt = "中转";
                break;
            case 40:
                $scope.cyzt = "外包";
                break;
            case 70:
                $scope.cyzt = "到货签单";
                break;
            case 90:
                $scope.cyzt = "已送货";
                break;
            case 100:
                $scope.cyzt = "客户签收";
                break;
            case 110:
                $scope.cyzt = "签单回报";
                break;
            case 120:
                $scope.cyzt = "签单接收";
                break;
            case 130:
                $scope.cyzt = "签单发放";
                break;
            case 140:
                $scope.cyzt = "回单";
                break;
            case 150:
                $scope.cyzt = "销单";
                break;
            default:
                break;
            }
            $scope.CydDetailData = cydDetailData;
        });

        CydProcessFactory.getProcess(id,sid,user_id,yh_id);
        //下拉更新
        $scope.doRefreshProcess=function(){
            CydProcessFactory.getProcess(id,sid,user_id,yh_id);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.$on('CydProcess.processUpdated',function(){
           var cydProcessData = CydProcessFactory.getProcessContent();
            $scope.processFirst=cydProcessData[0];
            var processData=[];
            var j;
            for(var i=1;i<cydProcessData.length;i++){
                j=i-1;
                processData[j]=cydProcessData[i];
            }
            $scope.processData=processData;
        });
        $scope.cyd_id=id;
        //拨号
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });

        };
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    })

    .controller('CydChangeCtrl',function($scope,$stateParams,CydChangeFactory,$ionicLoading,Storage,$ionicPopup,$cordovaActionSheet,$filter,$state,$timeout,$cordovaHdoitBle,AddCydFactory,$ionicScrollDelegate,ENV,$ionicHistory,$location){
        var id = $stateParams['cyd_id'];
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zdmc=Storage.get('zdmc');
        var zsxm=Storage.get('zsxm');
        var zd_id=Storage.get('zd_id');
        var dw_id=Storage.get("dw_id");
        var bmmc=Storage.get('bmmc');
        var url=Storage.get('url');
        var fwqbs=ENV.fwqbs;
        var mobile=/^((\+?86)|(\(\+86\)))?(13[0123456789][0-9]{8}|15[012356789][0-9]{8}|18[0123456789][0-9]{8}|147[0-9]{8}|149[0-9]{8}|145[0-9]{8}|17[0135678][0-9]{8})$/;
        var phone=/^(\d{3,4}\-)?[1-9]\d{6,7}$/;
        var scrollChangeWz=Storage.get('scrollChangeWz');
        $timeout(function(){
            if (scrollChangeWz!=null){
                $ionicScrollDelegate.$getByHandle('mainScroll').scrollTo(0,scrollChangeWz.top);
            }
        },100);
        $scope.tbzt={
            isCk:''
        };
        $scope.yfdfzt={
          isCk:''
        };
        $scope.dskdfzt={
          isCk:''
        };
        Storage.set('cyd_id_back',id);
        Storage.set('mc_back',$stateParams.mc);
        Storage.set('khmc_back',$stateParams.khmc);
        Storage.set('lxdh_back',$stateParams.lxdh);
        Storage.set('sflx_back',$stateParams.sflx);
        //费用加两位小数
        $(".numDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf=$(this).val();
            if($scope.cyd.yf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yf").css("color","red");
            }else {
                $("#yf").css("color","black");
            }
        });
        $(".xfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_xf=$(this).val();
            if($scope.cyd.yf_xf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yfxf").css("color","red");
            }else{
                $("#yfxf").css("color","black");
            }
        });
        $(".tfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_tf=$(this).val();
            if($scope.cyd.yf_tf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yftf").css("color","red");
            }else{
                $("#yftf").css("color","black");
            }
        });
        $(".hfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_hf=$(this).val();
            if($scope.cyd.yf_hf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yfhf").css("color","red");
            }else{
                $("#yfhf").css("color","black");
            }
        });
        $(".yjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_yj=$(this).val();
            if($scope.cyd.yf_yj>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yfyj").css("color","red");
            }else{
                $("#yfyj").css("color","black");
            }
        });
        $(".dkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_ycdk=$(this).val();
            if($scope.cyd.yf_ycdk>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yfdk").css("color","red");
            }else{
                $("#yfdk").css("color","black");
            }
        });
        $(".dshkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.dsk_hf=$(this).val();
            if($scope.cyd.dsk_hf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#dskhf").css("color","red");
            }else{
                $("#dskhf").css("color","black");
            }
        });
        $(".xxfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.xxf_xf=$(this).val();
            if($scope.cyd.xxf_xf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#xf").css("color","red");
            }else{
                $("#xf").css("color","black");
            }
        });
        $(".qfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.xxf_qf=$(this).val();
            if($scope.cyd.xxf_qf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#qf").css("color","red");
            }else{
                $("#qf").css("color","black");
            }
        });
        $(".shfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.shf1=$(this).val();
            if($scope.cyd.shf1>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#shf").css("color","red");
            }else{
                $("#shf").css("color","black");
            }
        });
        $(".thfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.thf=$(this).val();
            if($scope.cyd.thf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#thf").css("color","red");
            }else{
                $("#thf").css("color","black");
            }
        });
        $(".beDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.be=$(this).val();
            if($scope.cyd.be>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#be").css("color","red");
            }else{
                $("#be").css("color","black");
            }
            AddCydFactory.getbaofei(sid,user_id,yh_id,dw_id,$scope.cyd.be);
        });
        $(".bfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.bf=$(this).val();
            if($scope.cyd.bf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#bf").css("color","red");
            }else{
                $("#bf").css("color","black");
            }
        });
        $(".yfdfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.yf_df=$(this).val();
            if($scope.cyd.yf_df>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#yfdf").css("color","red");
            }else{
                $("#yfdf").css("color","black");
            }
        });
        $(".dskdfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.cyd.dsk_xf=$(this).val();
            if($scope.cyd.shf>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "费用金额不可超过3000000元",
                    duration: 1500
                });
                $("#dskxf").css("color","red");
            }else{
                $("#dskxf").css("color","black");
            }
        });
        $(".zlDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.cyd.zl=$(this).val();
            if($scope.cyd.zl>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "重量不可超过3000000吨",
                    duration: 1500
                });
                $("#zl").css("color","red");
            }else{
                $("#zl").css("color","black");
            }
        });
        $(".tjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.cyd.tj=$(this).val();
            if($scope.cyd.tj>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "体积不可超过3000000立方米",
                    duration: 1500
                });
                $("#tj").css("color","red");
            }else{
                $("#tj").css("color","black");
            }
        });
        //$("#jian").on("blur",function(){
        //    if(parseInt($(this).val())>=1000000){
        //        $(this).val("1000000");
        //    }
        //});
        $scope.$on('Baofeijisuan',function(){
            var bf=AddCydFactory.baofeiResult();
            $('#bf').val(bf+'.00');
        });
        //$scope.maxfy=function(fy){
        //    if(fy.toString().length>8){
        //        $ionicLoading.show({
        //            noBackdrop: true,
        //            template: "费用超过最大限制",
        //            duration: 1500
        //        });
        //    }
        //};
        $scope.changejian=function(a){
            if(a.toString().length>7){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "件数不可超过7位数",
                    duration: 1500
                });
                $("#jian").css("color","red");
            }
            else {
                $("#jian").css("color","black");
            }
        };
        $scope.changeqdfs=function(a){
          if (a!=null){
            if(a.toString().length>2){
              $ionicLoading.show({
                noBackdrop: true,
                template: "回单份数不可超过2位数",
                duration: 1500
              });
              $("#qdfs").css("color","red");
            }else {
              $("#qdfs").css("color","black");
            }
          }else {
            $scope.cyd.qdfs='';
          }
        };
        //添加费用项
        $scope.items=[] ;
        function addFyMenu(){
          if($scope.cyd.shf1==''||$scope.cyd.shf1==null||$scope.cyd.shf1==0)
          {
            $scope.items.push({index:1,name:'送货费'});
          }
          if($scope.cyd.thf==''||$scope.cyd.thf==null||$scope.cyd.thf==0)
          {
            $scope.items.push({index:2,name:'提货费'});
          }
          if($scope.cyd.be==''||$scope.cyd.be==null||$scope.cyd.be==0)
          {
            $scope.items.push({index:3,name:'保额'});
          }
          if($scope.cyd.bf==''||$scope.cyd.bf==null||$scope.cyd.bf==0)
          {
            $scope.items.push({index:4,name:'保费'});
          }
          if($scope.cyd.yf_df==''||$scope.cyd.yf_df==null||$scope.cyd.yf_df==0)
          {
            $scope.items.push({index:5,name:'运费垫付'});
          }
          if($scope.cyd.dsk_xf==''||$scope.cyd.dsk_xf==null||$scope.cyd.dsk_xf==0)
          {
            $scope.items.push({index:6,name:'代收款垫付'});
          }
        }
        //判断添加的费用是否有值
        function judgeYf(){
          if($scope.cyd.shf1!=''&&$scope.cyd.shf1!=null){
            $("#shfLab").removeClass("hide").addClass("showfy");
          }
          if($scope.cyd.thf!=''&&$scope.cyd.thf!=null){
            $("#thfLab").removeClass("hide").addClass("showfy");
          }
          if(($scope.cyd.be!=''&&$scope.cyd.be!=null)||$scope.cyd.tbzt=='1'){
            $("#beLab").removeClass("hide").addClass("showfy");
          }
          if($scope.cyd.bf!=''&&$scope.cyd.bf!=null){
            $("#bfLab").removeClass("hide").addClass("showfy");
          }
          if($scope.cyd.yf_df!=''&&$scope.cyd.yf_df!=null){
            $("#yfdfLab").removeClass("hide").addClass("showfy");
          }
          if($scope.cyd.dsk_xf!=''&&$scope.cyd.dsk_xf!=null){
            $("#dskdfLab").removeClass("hide").addClass("showfy");
          }
        }
        //判断是否为多笔付
        var fkType;
        function isDbf(){
          if(fkType=="6"){
            $("#yun").removeClass("showyf").addClass("hide");
            $("#xfLab").removeClass("hide").addClass("showyf");
            $("#tfLab").removeClass("hide").addClass("showyf");
            $("#hfLab").removeClass("hide").addClass("showyf");
            $("#dkLab").removeClass("hide").addClass("showyf");
            $("#yjLab").removeClass("hide").addClass("showyf");
          }else {
            $("#yun").removeClass("hide").addClass("showyf");
            $("#xfLab").removeClass("show").addClass("hide");
            $("#tfLab").removeClass("show").addClass("hide");
            $("#hfLab").removeClass("show").addClass("hide");
            $("#dkLab").removeClass("show").addClass("hide");
            $("#yjLab").removeClass("show").addClass("hide");
          }
        }
        var bzmc=$stateParams['bzlx'];
        if (Storage.get('cydData')!=null){
            $scope.cyd=Storage.get('cydData');
            addFyMenu();
            if($stateParams['mc']!=''){
                $scope.cyd.bzlx=$stateParams['mc'];
            }
            $(".zlDot").val($scope.cyd.zl);
            $(".tjDot").val($scope.cyd.tj);
            $(".numDot").val($scope.cyd.yf);
            $(".xfDot").val($scope.cyd.yf_xf);
            $(".tfDot").val($scope.cyd.yf_tf);
            $(".hfDot").val($scope.cyd.yf_hf);
            $(".yjDot").val($scope.cyd.yf_yj);
            $(".dkDot").val($scope.cyd.yf_ycdk);
            $(".dshkDot").val($scope.cyd.dsk_hf);
            $(".xxfDot").val($scope.cyd.xxf_xf);
            $(".qfDot").val($scope.cyd.xxf_qf);
            $(".shfDot").val($scope.cyd.shf);
            $(".thfDot").val($scope.cyd.thf);
            $(".beDot").val($scope.cyd.be);
            $(".bfDot").val($scope.cyd.bf);
            $(".yfdfDot").val($scope.cyd.yf_df);
            $(".dskdfDot").val($scope.cyd.dsk_xf);
            $("#ddz option:selected").val($scope.cyd.shbm_id);
            $("#ddz option:selected").text($scope.cyd.shbmmc);
            judgeYf();
            fkType=Storage.get('fkfs');
            isDbf();
            $timeout(function(){
                $("#fkType").val(Storage.get('fkfs'));
                $("#shType").val(Storage.get('shfs'));
                $("#yf").val(Storage.get('yf'));
            },800);
        }else {
            CydChangeFactory.getChangeDetail(id,sid,user_id,yh_id);
        }
        if ($stateParams.sflx==1){
            $scope.cyd.shrmc=$stateParams.khmc;
            $scope.cyd.shrdh= $stateParams.lxdh;
        }else if($stateParams.sflx==2){
            $scope.cyd.fhrmc=$stateParams.khmc;
            $scope.cyd.fhrdh= $stateParams.lxdh;
        }
        //初始化付款方式、送货方式、站点、包装
        if (Storage.get('fkfsData')!=null){
            $scope.fkfsChoose=Storage.get('fkfsData');
        }else {
            CydChangeFactory.queryFkfs(sid,yh_id);
        }
        if (Storage.get('shfsData')!=null){
            $scope.shfsChoose=Storage.get('shfsData');
        }else {
            CydChangeFactory.queryShfs(sid,yh_id);
        }
        CydChangeFactory.queryDdz(user_id,sid,yh_id,dw_id,zd_id);
        CydChangeFactory.queryBz(sid,dw_id,yh_id,user_id,bzmc);
        $scope.$on('CydChange.changeUpdated',function(){
            $scope.zdChoose=CydChangeFactory.getDdz();
            $scope.bzChoose=CydChangeFactory.getBzList().data;
            var shfsData=CydChangeFactory.getShfs();
            var fkfsData=CydChangeFactory.getFkfs();
            if(fkfsData!=undefined){
              $scope.fkfsChoose=fkfsData;
              Storage.set('fkfsData',$scope.fkfsChoose);
            }
            if (shfsData!=undefined){
              $scope.shfsChoose=shfsData;
              Storage.set('shfsData',$scope.shfsChoose);
            }
        });

        $scope.$on('CydDetail.changeUpdated',function(){
            var cydChangeData = CydChangeFactory.getChangeDetailContent();
            $scope.cyd=cydChangeData;
            console.log(cydChangeData);
            addFyMenu();
            $timeout(function() {
                $("#ddz").val(cydChangeData.shbm_id);
                $scope.cyd.bzlx=cydChangeData.bzlxmc;
                $("#shType").val(cydChangeData.shfs);
            },600);

            if($scope.cyd.cyzt=='20'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "该运单已装车！",
                    duration: 1000
                });
                $state.go('cydlist');
            }
            //投保状态
            if($scope.cyd.tbzt==''){
                $scope.tbzt.isCk=false;
            }else if($scope.cyd.tbzt=='1'){
                $scope.tbzt.isCk=true;
            }

            //运费垫付状态
            if($scope.cyd.yfdfzt=='1'){
              $scope.yfdfzt.isCk=false;
            }else if($scope.cyd.yfdfzt=='2'){
              $scope.yfdfzt.isCk=true;
            }

            //代收款垫付状态
            if($scope.cyd.dskdfzt=='1'){
              $scope.dskdfzt.isCk=false;
            }else if($scope.cyd.dskdfzt=='2'){
              $scope.dskdfzt.isCk=true;
            }

            if($scope.cyd.yf_xf==0&&$scope.cyd.yf_tf==0&&$scope.cyd.yf_hf==0&&$scope.cyd.yf_yj==0&&$scope.cyd.yf_ycdk==0){
                $scope.fkfs='现付';
                $scope.fkfs_id='1';
            }
            else if($scope.cyd.yf_xf!=0&&$scope.cyd.yf_tf==0&&$scope.cyd.yf_hf==0&&$scope.cyd.yf_yj==0&&$scope.cyd.yf_ycdk==0){
                $scope.fkfs='现付';
                $scope.fkfs_id='1';
                if (ENV.fwqbs == 'wh') {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $("#yfdf").val("");
                    $scope.cyd.yf_df='';
                }
            }
            else if($scope.cyd.yf_xf==0&&$scope.cyd.yf_tf!=0&&$scope.cyd.yf_hf==0&&$scope.cyd.yf_yj==0&&$scope.cyd.yf_ycdk==0){
                $scope.fkfs='提付';
                $scope.fkfs_id='2';
                if (ENV.fwqbs == 'wh') {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color", "#fff");
                }
            }
            else if($scope.cyd.yf_xf==0&&$scope.cyd.yf_tf==0&&$scope.cyd.yf_hf!=0&&$scope.cyd.yf_yj==0&&$scope.cyd.yf_ycdk==0){
                $scope.fkfs='回付';
                $scope.fkfs_id='3';
                if (ENV.fwqbs == 'wh') {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
            else if($scope.cyd.yf_xf==0&&$scope.cyd.yf_tf==0&&$scope.cyd.yf_hf==0&&$scope.cyd.yf_yj!=0&&$scope.cyd.yf_ycdk==0){
                $scope.fkfs='月结';
                $scope.fkfs_id='4';
                if (ENV.fwqbs == 'wh') {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
            else if($scope.cyd.yf_xf==0&&$scope.cyd.yf_tf==0&&$scope.cyd.yf_hf==0&&$scope.cyd.yf_yj==0&&$scope.cyd.yf_ycdk!=0){
                $scope.fkfs='代扣';
                $scope.fkfs_id='5';
                if (ENV.fwqbs == 'wh') {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
            else {
                $scope.fkfs='多笔付';
                $scope.fkfs_id='6';
                if (ENV.fwqbs == 'wh'&&$scope.cyd.yf_xf+$scope.cyd.yf_hf+$scope.cyd.yf_yj+$scope.cyd.yf_ycdk!=0) {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
            $timeout(function(){
                $("#fkType").val($scope.fkfs_id);
                fkType=$("#fkType option:selected").val();
                $("#yf").val($scope.cyd.yf_xf+$scope.cyd.yf_hf+$scope.cyd.yf_tf+$scope.cyd.yf_yj+$scope.cyd.yf_ycdk);
                isDbf();
            },600);
            if($scope.fkfs=='现付'){
                $("#yf").val($scope.cyd.yf_xf);
            }
            if($scope.fkfs=='提付'){
                $("#yf").val($scope.cyd.yf_tf);
                //$scope.cyd.yf=$scope.cyd.yf_tf;
            }
            if($scope.fkfs=='回付'){
                $("#yf").val($scope.cyd.yf_hf);
                //$scope.cyd.yf=$scope.cyd.yf_hf;
            }
            if($scope.fkfs=='月结'){
                $("#yf").val($scope.cyd.yf_yj);
                //$scope.cyd.yf=$scope.cyd.yf_yj;
            }
            if($scope.fkfs=='代扣'){
                $("#yf").val($scope.cyd.yf_ycdk);
                //$scope.cyd.yf=$scope.cyd.yf_ycdk;
            }
            judgeYf();
            switch ($scope.cyd.shfs){
                case 1 :
                    $scope.cyd.shfsmc = '自提';
                    break;
                case 2 :
                    $scope.cyd.shfsmc = '送货';
                    break;
                case 3 :
                    $scope.cyd.shfsmc = '上楼';
                    break;
                default :
                    break;
            }

        });
        //承运日期
        $scope.showCyrq = function() { //选择日期
            var optionsStart = {
                date:new Date($scope.cyd.cyrq),
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        $scope.cyd.cyrq = $filter('date')(d,'yyyy-MM-dd');
                    });
                }
            });
        };
        $("#yfdf").change(function () {
            if(ENV.fwqbs == 'wh'&&$("#fkType option:selected").val()!=2){
                if($scope.cyd.yf_df!=''&&$scope.cyd.yf_df!=null&&$scope.cyd.yf_df!=0){
                    $('#yf').attr("readonly", "readonly");
                    $('#yf').css("background-color","#F2F2F2");
                    $('#yfxf').attr("readonly", "readonly");
                    $('#yfxf').css("background-color","#F2F2F2");
                    $('#yfhf').attr("readonly", "readonly");
                    $('#yfhf').css("background-color","#F2F2F2");
                    $('#yfdk').attr("readonly", "readonly");
                    $('#yfdk').css("background-color","#F2F2F2");
                    $('#yfyj').attr("readonly", "readonly");
                    $('#yfyj').css("background-color","#F2F2F2");
                }
                else {
                    $('#yf').removeAttr("readonly");
                    $('#yf').css("background-color","#fff");
                    $('#yfxf').removeAttr("readonly");
                    $('#yfxf').css("background-color","#fff");
                    $('#yfhf').removeAttr("readonly");
                    $('#yfhf').css("background-color","#fff");
                    $('#yfyj').removeAttr("readonly");
                    $('#yfyj').css("background-color","#fff");
                    $('#yfdk').removeAttr("readonly");
                    $('#yfdk').css("background-color","#fff");
                }
            }
        });
        $("#yf").change(function(e) {
            if (ENV.fwqbs == 'wh'&&$("#fkType option:selected").val()!=2) {
                if ($scope.cyd.yf == '' || $scope.cyd.yf == null || $scope.cyd.yf == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                }
            }
        });
        //选择多笔付时显示现付、提付、回付、月结、代扣并隐藏运费，选择其他则反之；
        $("#fkType").change(function(e){
            var yfxf=$("#yfxf").val();
            var yfhf=$("#yfhf").val();
            var yftf=$("#yftf").val();
            var yfyj=$("#yfyj").val();
            var yfdk=$("#yfdk").val();
            if(yfxf==''||yfxf==null||yfxf==undefined||isNaN(yfxf)){
                yfxf=0;
            }
            if(yfhf==''||yfhf==null||yfhf==undefined||isNaN(yfhf)){
                yfhf=0;
            }
            if(yftf==''||yftf==null||yftf==undefined||isNaN(yftf)){
                yftf=0;
            }
            if(yfyj==''||yfyj==null||yfyj==undefined||isNaN(yfyj)){
                yfyj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined||isNaN(yfdk)){
                yfdk=0;
            }
            var yf=parseFloat(yfxf)+parseFloat(yftf)+parseFloat(yfyj)+parseFloat(yfhf)+parseFloat(yfdk);
            var fkType=$("#fkType option:selected").val();
            if (ENV.fwqbs == 'wh'&&fkType!=2) {
                if ($("#yf").val() == '' || $("#yf").val() == null || $("#yf").val() == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
                if($scope.cyd.yf_df==''||$scope.cyd.yf_df==null||$scope.cyd.yf_df==0){
                    $('#yf').removeAttr("readonly");
                    $('#yf').css("background-color","#fff");
                    $('#yfxf').removeAttr("readonly");
                    $('#yfxf').css("background-color","#fff");
                    $('#yfhf').removeAttr("readonly");
                    $('#yfhf').css("background-color","#fff");
                    $('#yfyj').removeAttr("readonly");
                    $('#yfyj').css("background-color","#fff");
                    $('#yfdk').removeAttr("readonly");
                    $('#yfdk').css("background-color","#fff");
                }
                else {
                    $('#yf').attr("readonly", "readonly");
                    $('#yf').css("background-color","#F2F2F2");
                    $('#yfxf').attr("readonly", "readonly");
                    $('#yfxf').css("background-color","#F2F2F2");
                    $('#yfhf').attr("readonly", "readonly");
                    $('#yfhf').css("background-color","#F2F2F2");
                    $('#yfdk').attr("readonly", "readonly");
                    $('#yfdk').css("background-color","#F2F2F2");
                    $('#yfyj').attr("readonly", "readonly");
                    $('#yfyj').css("background-color","#F2F2F2");
                }
            }
            switch (fkType){
                case "1":
                    if(yf==0){
                        $("#yf").val($scope.cyd.yf);
                        $("#yfxf").val($scope.cyd.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfxf").val(yf);
                    }
                    $scope.cyd.yf_xf=$("#yfxf").val();
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfxf").val(yf);
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.cyd.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "2":
                    if(yf==0){
                        $("#yf").val($scope.cyd.yf);
                        $("#yftf").val($scope.cyd.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yftf").val(yf);
                    }
                    $scope.cyd.yf_tf=$("#yftf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yfxf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').removeAttr("readonly");
                        $('#yfdf').css("background-color","#fff");
                        $('#yf').removeAttr("readonly");
                        $('#yf').css("background-color","#fff");
                    }
                    break;
                case "3":
                    if(yf==0){
                        $("#yf").val($scope.cyd.yf);
                        $("#yfhf").val($scope.cyd.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfhf").val(yf);
                    }
                    $scope.cyd.yf_hf=$("#yfhf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfxf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.cyd.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "4":
                    if(yf==0){
                        $("#yf").val($scope.cyd.yf);
                        $("#yfyj").val($scope.cyd.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfyj").val(yf);
                    }
                    $scope.cyd.yf_yj=$("#yfyj").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfxf").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.cyd.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "5":
                    if(yf==0){
                        $("#yf").val($scope.cyd.yf);
                        $("#yfdk").val($scope.cyd.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfdk").val(yf);
                    }
                    $scope.cyd.yf_ycdk=$("#yfdk").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_hf='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfxf").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.cyd.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
            }
            if(fkType=="6"){
                $("#yun").removeClass("showyf").addClass("hide");
                $("#xfLab").removeClass("hide").addClass("showyf");
                $("#tfLab").removeClass("hide").addClass("showyf");
                $("#hfLab").removeClass("hide").addClass("showyf");
                $("#dkLab").removeClass("hide").addClass("showyf");
                $("#yjLab").removeClass("hide").addClass("showyf");
                if(ENV.fwqbs=='wh'){
                    if(($scope.cyd.yf_xf!=''||$scope.cyd.yf_hf!=''||$scope.cyd.yf_ycdk!=''||$scope.cyd.yf_yj!='')&&($scope.cyd.yf_xf+$scope.cyd.yf_hf+$scope.cyd.yf_yj+$scope.cyd.yf_ycdk!=0)){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $("#yfdf").val("");
                        $scope.cyd.yf_df='';
                    }
                    else {
                        $('#yfdf').removeAttr("readonly");
                        $('#yfdf').css("background-color","#fff");
                        $('#yf').removeAttr("readonly");
                        $('#yf').css("background-color","#fff");
                    }
                }
            }else {
                $("#yun").removeClass("hide").addClass("showyf");
                $("#xfLab").removeClass("showyf").addClass("hide");
                $("#tfLab").removeClass("showyf").addClass("hide");
                $("#hfLab").removeClass("showyf").addClass("hide");
                $("#dkLab").removeClass("showyf").addClass("hide");
                $("#yjLab").removeClass("showyf").addClass("hide");
            }
        });
        $("#yfxf").change(function(e) {
            if (ENV.fwqbs == 'wh'&&$scope.cyd.yf_hf+$scope.cyd.yf_yj+$scope.cyd.yf_ycdk==0) {
                if ($scope.cyd.yf_xf == '' || $scope.cyd.yf_xf == null || $scope.cyd.yf_xf == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
        });
        $("#yfhf").change(function(e) {
            if (ENV.fwqbs == 'wh'&&$scope.cyd.yf_xf+$scope.cyd.yf_yj+$scope.cyd.yf_ycdk==0) {
                if ($scope.cyd.yf_hf == '' || $scope.cyd.yf_hf == null || $scope.cyd.yf_hf == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
        });
        $("#yfyj").change(function(e) {
            if (ENV.fwqbs == 'wh'&&$scope.cyd.yf_hf+$scope.cyd.yf_xf+$scope.cyd.yf_ycdk==0) {
                if ($scope.cyd.yf_yj == '' || $scope.cyd.yf_yj == null || $scope.cyd.yf_yj == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
        });
        $("#yfdk").change(function(e) {
            if (ENV.fwqbs == 'wh'&&$scope.cyd.yf_hf+$scope.cyd.yf_yj+$scope.cyd.yf_xf==0) {
                if ($scope.cyd.yf_ycdk == '' || $scope.cyd.yf_ycdk == null || $scope.cyd.yf_ycdk == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.cyd.yf_df='';
                    $("#yfdf").val("");
                }
            }
        });

        ///[{index:1,name:'送货费'},{index:2,name:'提货费'}, {index:3,name:'保额'},{index:4,name:'保费'},{index:5,name:'运费垫付'},{index:6,name:'代收款垫付'}];
        //添加费用菜单
        //$scope.items=[];
        //$scope.addMenu=function(){
        //    var options = {//选项
        //        title: '请选择需要添加的费用项',
        //        buttonLabels: ['送货费','提货费', '保额','保费','运费垫付','代收款垫付'],//按钮标签数给，索引从1开始
        //        addCancelButtonWithLabel: '取消',//取消按钮标签
        //        androidEnableCancelButton: true,//激活Android取消按钮
        //        winphoneEnableCancelButton: true//激活winphone取消按钮
        //    };
        //    document.addEventListener("deviceready", function () {
        //        $cordovaActionSheet.show(options)
        //            .then(function (btnIndex) {//成功后的回调
        //                var index = btnIndex;//返回所选按钮的索引
        //                switch(index) {
        //                    case 1:
        //                        $("#shfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    case 2:
        //                        $("#thfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    case 3:
        //                        $("#beLab").removeClass("hide").addClass("showfy");
        //                        $("#bfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    case 4:
        //                        $("#bfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    case 5:
        //                        $("#yfdfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    case 6:
        //                        $("#dskdfLab").removeClass("hide").addClass("showfy");
        //                        break;
        //                    default:
        //                        break;
        //                }
        //            });
        //    }, false);
        //};
        var popupForWarehouse;
        $scope.addMenu=function(){
            popupForWarehouse = $ionicPopup.alert({
                template: '<div class="list">                '+
                '  <ion-item ng-repeat="item in items" ng-click="removefy($index,item.index)" class="item item-icon-right" id="item.index"> '+
                '    <h2 >{{item.name}}<h2>                   '+
                '  </ion-item>                                      '+
                '</div>                                      ',
                title:'请选择需要添加的费用',
                scope: $scope,
                okText:'取消'
            });
        };

        $scope.removefy= function (r,i) {
            if(i!=3&&i!=4){
                $scope.items.splice(r,1);
            }
            else if(i==3){
                $scope.items.splice(r,2);
            }else if(i==4){
                $scope.items.splice(r-1,2);
            }
            switch(i) {
                case 1:
                    $("#shfLab").removeClass("hide").addClass("showfy");
                    break;
                case 2:
                    $("#thfLab").removeClass("hide").addClass("showfy");
                    break;
                case 3:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 4:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 5:
                    $("#yfdfLab").removeClass("hide").addClass("showfy");
                    break;
                case 6:
                    $("#dskdfLab").removeClass("hide").addClass("showfy");
                    break;
                default:
                    break;
            }
            popupForWarehouse.close();
        };
        $scope.holdonRemove=function(i){
            var receipt = $ionicPopup.confirm({
                title : '是否删除此费用项',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    switch(i) {
                        case 1:
                            $("#shfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(0,0,{index:1,name:'送货费'});
                            $scope.cyd.shf1='';
                            break;
                        case 2:
                            $("#thfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(1,0,{index:2,name:'提货费'});
                            $scope.cyd.thf='';
                            break;
                        case 3:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.cyd.be='';
                            $scope.cyd.isCk='';
                            $scope.cyd.bf='';
                            break;
                        case 4:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.cyd.be='';
                            $scope.cyd.isCk='';
                            $scope.cyd.bf='';
                            break;
                        case 5:
                            $("#yfdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(4,0,{index:5,name:'运费垫付'});
                            $scope.cyd.yf_df='';
                            break;
                        case 6:
                            $("#dskdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(5,0,{index:6,name:'代收款垫付'});
                            $scope.cyd.dsk_xf='';
                            break;
                        default:
                            break;
                    }
                }else{
                    console.log('取消');
                }
            });
        };
        //选择包装
        $scope.chooseBz = function () {
            Storage.set('scrollChangeWz',$ionicScrollDelegate.$getByHandle('mainScroll').getScrollPosition());
            $scope.cyd.shbm_id=$("#ddz option:selected").val();
            $scope.cyd.shbmmc=$("#ddz option:selected").text();
            Storage.set('cydData',$scope.cyd);
            Storage.set('fkfs',$("#fkType option:selected").val());
            Storage.set('shfs',$("#shType option:selected").val());
            Storage.set('yf',$("#yf").val());
            $state.go("changeBz");
        };
        $scope.choose=function(mc){
            $state.go("cydChange",{mc:mc});
            $timeout(function(){
                Storage.set('scrollChangeWz',null);
            },160);
        };
        //选择联系人
        $scope.chooseLinkman = function (htmlbs,sflx) {
            Storage.set('scrollChangeWz',$ionicScrollDelegate.$getByHandle('mainScroll').getScrollPosition());
            $scope.cyd.shbm_id=$("#ddz option:selected").val();
            $scope.cyd.shbmmc=$("#ddz option:selected").text();
            $timeout(function(){
                Storage.set('cydData',$scope.cyd);
                Storage.set('fkfs',$("#fkType option:selected").val());
                Storage.set('shfs',$("#shType option:selected").val());
                Storage.set('yf',$("#yf").val());
            },160);
            $state.go("linkman",{htmlbs:htmlbs,sflx:sflx});
        };
        $scope.goBack=function(){
            var receipt = $ionicPopup.confirm({
                title: '是否退出当前页面',
                cancelText: '取消',
                cancelType: 'button-stable',
                okText: '确定'
            });
            receipt.then(function (r) {
                if (r) {
                    $state.go("cydlist");
                    Storage.set('scrollChangeWz', null);
                    Storage.set('cydData', null);
                    Storage.set('fkfs', null);
                    Storage.set('shfs', null);
                } else {
                    console.log('取消');
                }
            });
        };
        $scope.bzlx={
            'bz':''
        };
        $scope.goSearch=function(){
            $state.go('changeBzSearch');
        };
        $scope.Search=function(bzlx){
            if(bzlx==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入包装类型",
                    duration: 1500
                });
            }else {
                $state.go('changeBz',{bzlx:bzlx});
            }
        };
        //下拉更新
        $scope.doRefresh=function(){
            CydChangeFactory.queryBz(sid,dw_id,yh_id,user_id,bzmc);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉更新
        $scope.loadMore=function(){
            CydChangeFactory.getMoreBz(sid,dw_id,yh_id,user_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        //下一页
        $scope.hasNextPage = function() {
            return CydChangeFactory.hasNextPage();
        };
        //点击保存承运单
        $scope.saveCyd = function(){
            $scope.cyd.cydh=$("#cydhCode").val();
            var ddz=$("#ddz option:selected").val();
            var ddzStr=$("#ddz option:selected").text();
            var shType=$("#shType option:selected").val();
            $("#save").attr("disabled", true);
            $("#print").attr("disabled", true);
            $scope.cyd.shbm_id=ddz;
            $scope.cyd.shbmmc=ddzStr;
            $scope.cyd.shfs = shType;

            //投保状态
            if( $scope.tbzt.isCk==true){
                $scope.cyd.tbzt='1';
            }else {
                $scope.cyd.tbzt='';
            }
            //运费垫付状态
            if( $scope.yfdfzt.isCk==true){
              $scope.cyd.yfdfzt='2';
            }else {
              $scope.cyd.yfdfzt='1';
            }
            //代收款垫付状态
            if( $scope.dskdfzt.isCk==true){
              $scope.cyd.dskdfzt='2';
            }else {
              $scope.cyd.dskdfzt='1';
            }
            if($scope.cyd.sfszjs=='1'&&$scope.cyd.cydh!=""){
                $scope.cyd.cydh=$scope.cyd.cydh+"-"+$scope.cyd.sl;
            }
            //判断承运类型
            if($scope.cyd.shbmmc!=''&&$scope.cyd.mdd==''){
                $scope.cyd.cylx='1';
            }else if($scope.cyd.shbmmc!=''&&$scope.cyd.mdd!=''){
                $scope.cyd.cylx='2';
            }else if($scope.cyd.shbmmc==''&&$scope.cyd.mdd!=''){
                $scope.cyd.cylx='3';
            }
            //判断是否回单
            if($scope.cyd.qdfs!=''&&$scope.cyd.qdfs!=0){
                $scope.cyd.sfyqd=1;
            }else {
                $scope.cyd.sfyqd=2;
            }
            //判断付款方式
            if ($("#yf").val()==''){
              $("#yf").val(0);
            }
            switch ($("#fkType option:selected").text()){
                case '现付':
                    $scope.cyd.yf_xf=parseFloat($("#yf").val()).toFixed(2);
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $("#yfdf").val("");
                        $scope.cyd.yf_df='';
                    }
                    break;
                case '回付':
                    $scope.cyd.yf_hf=parseFloat($("#yf").val()).toFixed(2);
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfxf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $("#yfdf").val("");
                        $scope.cyd.yf_df='';
                    }
                    break;
                case '提付':
                    $scope.cyd.yf_tf=parseFloat($("#yf").val()).toFixed(2);
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yfxf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    break;
                case '月结':
                    $scope.cyd.yf_yj=parseFloat($("#yf").val()).toFixed(2);
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfxf").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $("#yfdf").val("");
                        $scope.cyd.yf_df='';
                    }
                    break;
                case '代扣':
                    $scope.cyd.yf_ycdk=parseFloat($("#yf").val()).toFixed(2);
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfxf").val("");
                    if(ENV.fwqbs=='wh'){
                        $("#yfdf").val("");
                        $scope.cyd.yf_df='';
                    }
                    break;
                case '多笔付':
                    break;
            }
            if($scope.cyd.cydh==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入承运单号",
                    duration: 1500
                })
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#cydhCode").focus();
                },160);
            }
            else if($scope.cyd.sl==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入件数",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#jian").focus();
                },160);
            }else if($scope.cyd.yf>3000000|| $scope.cyd.yf_xf>3000000|| $scope.cyd.yf_tf>3000000|| $scope.cyd.yf_hf>3000000 || $scope.cyd.yf_yj>3000000|| $scope.cyd.yf_ycdk>3000000|| $scope.cyd.dsk_hf>3000000|| $scope.cyd.xxf_xf>3000000|| $scope.cyd.xxf_qf>3000000|| $scope.cyd.shf>3000000|| $scope.cyd.thf>3000000|| $scope.cyd.be>3000000|| $scope.cyd.bf>3000000|| $scope.cyd.yf_df>3000000|| $scope.cyd.dsk_xf>3000000|| $scope.cyd.zl>3000000|| $scope.cyd.tj>3000000|| $scope.cyd.qdfs.toString().length>2|| $scope.cyd.sl.toString().length>7){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合理的费用或数量、体积",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
            }else if(ddzStr==""&&$scope.cyd.mdd==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择到达站或目的地",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#ddz").focus();
                },160);
            } else if($scope.cyd.fhrdh==$scope.cyd.shrdh&&($scope.cyd.fhrdh!=''||$scope.cyd.shrdh!='')){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "收发货电话相同",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#shrdhval").focus();
                },160);
            }else if(!mobile.test($scope.cyd.shrdh)&&!phone.test($scope.cyd.shrdh)&&$scope.cyd.shrdh!=''&&$('#shrdhval').val()!=''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合法电话",
                    duration: 600
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#shrdhval").focus();
                },160);
            }else if(!mobile.test($scope.cyd.fhrdh)&&!phone.test($scope.cyd.fhrdh)&&$scope.cyd.fhrdh!=''&&$('#fhrdhval').val()!=''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合法电话",
                    duration: 600
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#fhrdhval").focus();
                },160);
            }else if($scope.yfdfzt.isCk==true&&$scope.cyd.yf_df==''){
              $ionicLoading.show({
                noBackdrop: true,
                template: "请输入运费垫付",
                duration: 600
              });
              $("#save").attr("disabled", false);
              $("#print").attr("disabled", false);
              $timeout(function(){
                $("#yfdf").focus();
              },160);
            }else if($scope.dskdfzt.isCk==true&&$scope.cyd.dsk_xf==''){
              $ionicLoading.show({
                noBackdrop: true,
                template: "请输入代收款垫付",
                duration: 600
              });
              $("#save").attr("disabled", false);
              $("#print").attr("disabled", false);
              $timeout(function(){
                $("#dskxf").focus();
              },160);
            }
            else {
                CydChangeFactory.checkCydhExist(sid,user_id,dw_id,yh_id,$scope.cyd.cydh,$scope.cyd.cyd_id);
                $scope.$on('checkCydh',function(){
                    var result=CydChangeFactory.getCount();
                    var count=result.ACTION_INFO.RESULT_LIST[0].cn;
                    if(result.ACTION_RETURN_CODE=='000000'){
                        if(count=='0'){
                            CydChangeFactory.saveCyd(dw_id,$scope.cyd,yh_id,sid,user_id,zd_id,$scope.cyd.cyrq,bmmc,zsxm);
                            $scope.$on('CydSaveChange.saveChangeUpdated',function(){
                                var result=CydChangeFactory.getResult();
                                if(result.ACTION_RETURN_CODE=='000000'){
                                    $ionicLoading.show({
                                        noBackdrop: true,
                                        template: "保存成功！",
                                        duration: 1500
                                    });
                                    Storage.set('scrollChangeWz',null);
                                    Storage.set('cydData',null);
                                    Storage.set('fkfs',null);
                                    Storage.set('shfs',null);
                                    $state.go("cydlist");
                                }else{
                                    $ionicLoading.show({
                                        noBackdrop: true,
                                        template: "保存失败！",
                                        duration: 1500
                                    });
                                    $("#save").attr("disabled", false);
                                    $("#print").attr("disabled", false);
                                }
                            });
                        }else {
                            $("#cydhCode").focus();
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "承运单号已存在！",
                                duration: 1500
                            });
                            $("#save").attr("disabled", false);
                            $("#print").attr("disabled", false);
                        }
                    }
                });
            }
        };

        //打印部分
        var dyfs,hour,time,today;
        var bmmc=Storage.get('bmmc');
        $scope.dy={
            sfdywlgscg:'',
            sfdyfhrcg:'',
            sfdyhwbt:'',
            hwbtdyfs:'',
            hwdysl:'',
            fhrcgsl:1,
            wlgscgsl:1,
            read:false,
            qsh:1,
            sfdyxxf:''
        };
        //初始化打印方式
        function initDyfs(){
            CydChangeFactory.fetchPrintYd(sid,user_id,yh_id,zd_id);
            $scope.$on('QueryYd.PrintSet',function(){
                var set=CydChangeFactory.getSetYd();
                if(set.ACTION_RETURN_CODE=='000000'){
                    var list=set.ACTION_INFO;
                    if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='1'){
                        dyfs='1'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='1'){
                        dyfs='2'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='1'){
                        dyfs='3'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='0'){
                        dyfs='4'
                    }else if(list.sfdyfhrcg=='0'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='0'){
                        dyfs='5'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='1'){
                        dyfs='6'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='0'&&list.sfdywlgscg=='0'){
                        dyfs='7'
                    }else if(list.sfdyfhrcg=='1'&&list.sfdyhwbt=='1'&&list.sfdywlgscg=='0'){
                        dyfs='8'
                    }
                    $scope.dy.sfdyfhrcg=list.sfdyfhrcg;
                    $scope.dy.sfdywlgscg=list.sfdywlgscg;
                    $scope.dy.sfdyhwbt=list.sfdyhwbt;
                    $scope.dy.hwbtdyfs=list.hwbtdyxw;
                    $scope.title=list.gsmc;
                    $scope.zrtw=list.bz;
                    $scope.kfdh=list.kfdh;
                    $scope.dy.sfdyxxf=list.sfdyxxf;
                }
            });
        }

        function readyToPrintGscg(){
            var site='';
            var district="";
            var sjdh='';
            var ds='代收:';
            var dsje=$scope.cyd.dsk_hf+'元';
            var dfje=$scope.cyd.dsk_xf+'元';
            var daofje=$scope.cyd.yf_tf+'元';
            var xxf_xfje=$scope.cyd.xxf_xf+'元';
            var xxf_qfje=$scope.cyd.xxf_qf+'元';
            var hwsl=$scope.cyd.sl+'件';
            var hwmc=$scope.cyd.hwmc;
            var yfdfje=$scope.cyd.yf_df+'元';
            var yf_hf,yf_xf,yf_tf,yf_yj,yfdk,sjthf,shf1,yf_df,bf;
            yf_hf=(!isNaN($scope.cyd.yf_hf))?$scope.cyd.yf_hf:0;
            yf_xf=(!isNaN($scope.cyd.yf_xf))?$scope.cyd.yf_xf:0;
            yf_tf=(!isNaN($scope.cyd.yf_tf))?$scope.cyd.yf_tf:0;
            yf_yj=(!isNaN($scope.cyd.yf_yj))?$scope.cyd.yf_yj:0;
            yfdk=(!isNaN($scope.cyd.yf_ycdk))?$scope.cyd.yf_ycdk:0;
            yf_df=(!isNaN($scope.cyd.yf_df))?$scope.cyd.yf_df:0;
            shf1=(!isNaN($scope.cyd.shf1))?$scope.cyd.shf1:0;
            sjthf=(!isNaN($scope.cyd.thf))?$scope.cyd.thf:0;
            bf=(!isNaN($scope.cyd.bf))?$scope.cyd.bf:0;

            var yuan='元';
            if(yf_hf==''||yf_hf==null||yf_hf==undefined){
                yf_hf=0;
            }
            if(yf_xf==''||yf_xf==null||yf_xf==undefined){
                yf_xf=0;
            }
            if(yf_tf==''||yf_tf==null||yf_tf==undefined){
                yf_tf=0;
            }
            if(yf_yj==''||yf_yj==null||yf_yj==undefined){
                yf_yj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined){
                yfdk=0;
            }
            if(bf==''||bf==null||bf==undefined){
                bf=0;
            }
            if(shf1==''||shf1==null||shf1==undefined){
                shf1=0;
            }
            if(yf_df==''||yf_df==null||yf_df==undefined){
                yf_df=0;
            }
            if(sjthf==''||sjthf==null||sjthf==undefined){
                sjthf=0;
            }
            var yfje=(parseFloat(yf_hf)+parseFloat(yf_xf)+parseFloat(yf_tf)+parseFloat(yf_yj)+parseFloat(yfdk)+parseFloat(shf1)+parseFloat(sjthf)+parseFloat(bf)-parseFloat(yf_df)).toFixed(2);

            if(yfje==0||yfje==''||yfje==null){
                yfje=0;
            }


            var shrmc=$scope.cyd.shrmc+'';
            var fhrmc=$scope.cyd.fhrmc+'';
            var shrdh=$("#shrdhval").val();
            var fhrdh=$("#fhrdhval").val();
            var df='代收垫付:';
            var dou1=',';
            var dou2=',';
            var dou3=',';
            var dou4=',';
            var yf='运费:';
            var daof='到付:';
            var yfdf='运费垫付:';
            var xxf_xf='现返:';
            var xxf_qf='欠返:';
            var ddz=$("#ddz option:selected").text();
            var shType=$("#shType option:selected").text();
            if($scope.cyd.mdd==""&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz;
               district=ddz;
               sjdh=$scope.ddzdh;
            }else if($scope.cyd.mdd!=''&&ddz==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
              district=$scope.cyd.mdd;
              sjdh='';
            }else if($scope.cyd.mdd!=''&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz+" → "+$scope.cyd.mdd;
              district=ddz;
              sjdh=$scope.ddzdh;
            }
            if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf==''){
                dsje='0元';
                dfje='';
                dou1='';
                df='';
            }else if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf!=''){
                dsje='0元';
                dfje=$scope.cyd.dsk_xf+'元';
                dou1=',';
            }else if($scope.cyd.dsk_hf!=''&&$scope.cyd.dsk_xf==''){
                dou1='';
                df='';
                dfje='';
            }
            if($scope.cyd.yf_hf+$scope.cyd.yf_tf+$scope.cyd.yf_ycdk+$scope.cyd.yf_xf+$scope.cyd.yf_yj==''){
                yfje=0
            }
            if ($scope.dy.sfdyxxf==0){
              xxf_xf='';
              xxf_xfje='';
              xxf_qf='';
              xxf_qfje='';
              dou2='';
            }
            if($scope.cyd.xxf_xf==''){
                xxf_xf='';
                xxf_xfje='';
            }
            if ($scope.cyd.xxf_qf==''){
                xxf_qf='';
                xxf_qfje='';
                dou2='';
            }
            if($scope.cyd.yf_df==''){
                dou3='';
                yfdf='';
                yfdfje='';
            }
            if($scope.cyd.yf_tf==''){
                daofje='0元';
            }
            var dou5=',';
            var hdfs='回单:';
            var qdfs=$scope.cyd.qdfs+'份';
            if($scope.cyd.qdfs==''){
                dou5='';
                hdfs='';
                qdfs='';
            }
            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":430};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var yundanCode={"x":10,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("运单号：",yundanCode);

            //运单号
            var yundanCode1={"x":110,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);

            var date={"x":320,"y":65,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(today,date);

            //var second={"x":460,"y":17.5,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("第二联",second);
            var kefuPhone={"x":420,"y":17.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);

            var wlgslc={"x":490,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("存根",wlgslc);
            //始发站
            var route1={"x":10,"y":105,"textSize":15,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site+'  '+hwmc+hwsl,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":90,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);

            //送货方式
            var shfs={"x":380,"y":145,"textSize":3,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shType,shfs);

            //收货人
            var shf={"x":10,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("收货方",shf);

            //收货人名称
            var recieveP={"x":110,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrmc,recieveP);

            //收货人电话
            var shPhone={"x":210,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrdh,shPhone);

            //发货人
            var fhf={"x":10,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方",fhf);

            //发货人名称
            var sendP={"x":110,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrmc,sendP);

            //发货人电话
            var fhPhone={"x":210,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrdh,fhPhone);

            //代收、代收垫付
            var dsdf={"x":10,"y":215,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(ds+dsje+dou1+df+dfje,dsdf);

            //运费、到付、运费垫付,hgk
            var yfdfdf={"x":10,"y":245,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yf+yfje+yuan+dou2+daof+daofje+dou5+hdfs+qdfs,yfdfdf);

            //声明价值
            var note={"x":10,"y":285,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yfdf+yfdfje+dou3+xxf_xf+ xxf_xfje+dou2+xxf_qf+xxf_qfje,note);

            //var kefuPhone={"x":395,"y":245,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("电话："+$scope.kfdh,kefuPhone);
            //到站信息
            var mddNote={"x":10,"y":315,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(district+' '+ sjdh,mddNote);

            //备注
            var warn1={"x":10,"y":345,"width":520,"height":64,"fontSize":2,"rotate":0,"bold":1,"underline":false,"reverse":false};
            $cordovaHdoitBle.multilineText($scope.zrtw,warn1);

            //开票人
            var kpr={"x":380,"y":315,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("开票人："+zsxm,kpr);
            //扫码跟踪
            var xyt={"x":470,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("扫码跟踪",xyt);
            //打印二维码
            var textOption2 = {"x":460,"y":90,"size":3};
            $cordovaHdoitBle.printQRcodeZhike(url+"/jsp/scanning.jsp?cyd_id="+$scope.cyd.cyd_id+"&bs="+fwqbs,textOption2);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        function readyToPrintFhrcg(){
            var site='';
            var district="";
            var sjdh='';
            var ds='代收:';
            var dsje=$scope.cyd.dsk_hf+'元';
            var dfje=$scope.cyd.dsk_xf+'元';
            var daofje=$scope.cyd.yf_tf+'元';
            var xxf_xfje=$scope.cyd.xxf_xf+'元';
            var xxf_qfje=$scope.cyd.xxf_qf+'元';
            var hwsl=$scope.cyd.sl+'件';
            var hwmc=$scope.cyd.hwmc;
            var yfdfje=$scope.cyd.yf_df+'元';
            var yf_hf,yf_xf,yf_tf,yf_yj,yfdk,sjthf,shf1,yf_df,bf;
            yf_hf=(!isNaN($scope.cyd.yf_hf))?$scope.cyd.yf_hf:0;
            yf_xf=(!isNaN($scope.cyd.yf_xf))?$scope.cyd.yf_xf:0;
            yf_tf=(!isNaN($scope.cyd.yf_tf))?$scope.cyd.yf_tf:0;
            yf_yj=(!isNaN($scope.cyd.yf_yj))?$scope.cyd.yf_yj:0;
            yfdk=(!isNaN($scope.cyd.yf_ycdk))?$scope.cyd.yf_ycdk:0;
            yf_df=(!isNaN($scope.cyd.yf_df))?$scope.cyd.yf_df:0;
            shf1=(!isNaN($scope.cyd.shf1))?$scope.cyd.shf1:0;
            sjthf=(!isNaN($scope.cyd.thf))?$scope.cyd.thf:0;
            bf=(!isNaN($scope.cyd.bf))?$scope.cyd.bf:0;

            var yuan='元';
            if(yf_hf==''||yf_hf==null||yf_hf==undefined){
                yf_hf=0;
            }
            if(yf_xf==''||yf_xf==null||yf_xf==undefined){
                yf_xf=0;
            }
            if(yf_tf==''||yf_tf==null||yf_tf==undefined){
                yf_tf=0;
            }
            if(yf_yj==''||yf_yj==null||yf_yj==undefined){
                yf_yj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined){
                yfdk=0;
            }
            if(bf==''||bf==null||bf==undefined){
                bf=0;
            }
            if(shf1==''||shf1==null||shf1==undefined){
                shf1=0;
            }
            if(yf_df==''||yf_df==null||yf_df==undefined){
                yf_df=0;
            }
            if(sjthf==''||sjthf==null||sjthf==undefined){
                sjthf=0;
            }
            var yfje=(parseFloat(yf_hf)+parseFloat(yf_xf)+parseFloat(yf_tf)+parseFloat(yf_yj)+parseFloat(yfdk)+parseFloat(shf1)+parseFloat(sjthf)+parseFloat(bf)-parseFloat(yf_df)).toFixed(2);

            if(yfje==0||yfje==''||yfje==null){
                yuan='元';
            }
            var shrmc=$scope.cyd.shrmc+'';
            var fhrmc=$scope.cyd.fhrmc+'';
            var shrdh=$("#shrdhval").val();
            var fhrdh=$("#fhrdhval").val();
            var df='代收垫付:';
            var dou1=',';
            var dou2=',';
            var dou3=',';
            var dou4=',';
            var yf='运费:';
            var daof='到付:';
            var yfdf='运费垫付:';
            var xxf_xf='现返:';
            var xxf_qf='欠返:';
            var ddz=$("#ddz option:selected").text();
            var shType=$("#shType option:selected").text();
            if($scope.cyd.mdd==""&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz;
                district=ddz;
                sjdh=$scope.ddzdh;
            }else if($scope.cyd.mdd!=''&&ddz==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
                district=$scope.cyd.mdd;
                sjdh='';
            }else if($scope.cyd.mdd!=''&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz+" → "+$scope.cyd.mdd;
                district=ddz;
                sjdh=$scope.ddzdh;
            }
            if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf==''){
                dsje='0元';
                dfje='';
                dou1='';
                df='';
            }else if($scope.cyd.dsk_hf==''&&$scope.cyd.dsk_xf!=''){
                dsje='0元';
                dfje=$scope.cyd.dsk_xf+'元';
                dou1='，';
            }else if($scope.cyd.dsk_hf!=''&&$scope.cyd.dsk_xf==''){
                dou1='';
                df='';
                dfje='';
            }
            if ($scope.dy.sfdyxxf==0){
                xxf_xf='';
                xxf_xfje='';
                xxf_qf='';
                xxf_qfje='';
                dou2='';
            }
            if($scope.cyd.xxf_xf==''){
                xxf_xf='';
                xxf_xfje='';
            }
            if ($scope.cyd.xxf_qf==''){
                xxf_qf='';
                xxf_qfje='';
                dou2='';
            }
            if($scope.cyd.yf_df==''){
                dou3='';
                yfdf='';
                yfdfje='';
            }
            if($scope.cyd.yf_tf==''){
                daofje='0元';
            }
            //签回单
            var dou5=',';
            var hdfs='回单:';
            var qdfs=$scope.cyd.qdfs+'份';
            if($scope.cyd.qdfs==''){
                dou5='';
                hdfs='';
                qdfs='';
            }
            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":400};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var yundanCode={"x":10,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("运单号：",yundanCode);

            //运单号
            var yundanCode1={"x":110,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);

            //var first={"x":460,"y":17.5,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("第一联",first);

            var date={"x":320,"y":65,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(today,date);

            var fhrlc={"x":460,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方留存",fhrlc);
            //始发站
            var route1={"x":10,"y":105,"textSize":15,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site+'  '+hwmc+hwsl,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":90,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);

            //送货方式
            var shfs={"x":380,"y":145,"textSize":3,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shType,shfs);

            //收货人
            var shf={"x":10,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("收货方",shf);

            //收货人名称
            var recieveP={"x":110,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrmc,recieveP);

            //收货人电话
            var shPhone={"x":210,"y":145,"textSize":6,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(shrdh,shPhone);
            //发货人
            var fhf={"x":10,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("发货方",fhf);

            //发货人名称
            var sendP={"x":110,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrmc,sendP);

            //发货人电话
            var fhPhone={"x":210,"y":175,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(fhrdh,fhPhone);

            //代收、代收垫付
            var dsdf={"x":10,"y":215,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(ds+dsje+dou1+df+dfje,dsdf);

            //运费、到付
            var yfdfdf={"x":10,"y":245,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yf+yfje+yuan+dou2+daof+daofje+dou5+hdfs+qdfs,yfdfdf);

            //货物名称件数声明价值、运费垫付
            var note={"x":10,"y":285,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(yfdf+yfdfje+dou3+xxf_xf+ xxf_xfje+dou2+xxf_qf+xxf_qfje,note);

            var kefuPhone={"x":420,"y":17.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);

            //到站信息
            var mddNote={"x":10,"y":315,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike(district+" "+sjdh,mddNote);

            //备注
            var warn1={"x":10,"y":345,"width":520,"height":64,"fontSize":2,"rotate":0,"bold":1,"underline":false,"reverse":false};
            $cordovaHdoitBle.multilineText($scope.zrtw,warn1);

            //开票人
            var kpr={"x":400,"y":315,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("开票人："+zsxm,kpr);
            //关注信运通
            //var xyt={"x":440,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            //$cordovaHdoitBle.printTextZhike("关注信运通",xyt);
            //扫码跟踪
            var xyt={"x":470,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike("扫码跟踪",xyt);
            //打印二维码
            var textOption2 = {"x":460,"y":90,"size":3};
            $cordovaHdoitBle.printQRcodeZhike(url+"/jsp/scanning.jsp?cyd_id="+$scope.cyd.cyd_id+"&bs="+fwqbs,textOption2);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        function readyToPrintHwbq(hwbh){
            var site='';
            var ddz=$("#ddz option:selected").text();
            var shType=$("#shType option:selected").text();
            if($scope.cyd.mdd==""&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz;
            }else if($scope.cyd.mdd!=''&&ddz==''){
                site=$scope.cyd.lrbmmc+" → "+$scope.cyd.mdd;
            }else if($scope.cyd.mdd!=''&&ddz!=''){
                site=$scope.cyd.lrbmmc+" → "+ddz+" → "+$scope.cyd.mdd;
            }

            //设置打印机页面
            var startPageOption = {"pageWidth":600,"pageHeight":430};
            $cordovaHdoitBle.startPageZhike(startPageOption);
            //打印文本
            var gsNmae = {"x":10,"y":-5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.title,gsNmae);

            var kefuPhone={"x":420,"y":7.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.kfdh,kefuPhone);

            //运单号
            var yundanCode1={"x":10,"y":80,"textSize":20,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh,yundanCode1);
            //始发站
            var route1={"x":10,"y":160,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike(site,route1);

            //到达站（目的地）
            //var route2={"x":110,"y":165,"textSize":20,"fontName":"宋体","bold":true,"rotate":false};
            //$cordovaHdoitBle.printTextZhike(site,route2);
            //货名、件数、收货人姓名
            var hm = {"x":10,"y":235,"textSize":15,"fontName":"黑体","bold":true,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.hwmc+$scope.cyd.sl+'件，第'+hwbh+"件   "+$scope.cyd.shrmc,hm);

            //打印一维码

            var textOption3 = {"x":10,"y":280,"type":'128','width':1,'height':85,'rotate':false};
            $cordovaHdoitBle.printBarcodeZhike($scope.cyd.cydh,textOption3);

            var hwxx={"x":10,"y":380,"textSize":1,"fontName":"宋体","bold":false,"rotate":false};
            $cordovaHdoitBle.printTextZhike($scope.cyd.cydh+'-'+hwbh+'  '+time,hwxx);
            $cordovaHdoitBle.doPrint();
            $cordovaHdoitBle.gotoMarklabel();
        }

        //不足n位前补0
        function padLeft(str, lenght) {
            if (str.length >= lenght)
                return str;
            else
                return padLeft("0" + str, lenght);
        }
        //打印
        initDyfs();
        var hwbh='';
        var isConeccted;
        function getBleName(){
            //蓝牙设备名称
            $cordovaHdoitBle.bluetoothName(function(success){
                if(success!=null){
                    isConeccted='1';
                }
                else {
                    isConeccted='0';
                }
            },function(err){
                isConeccted='0';
            });
        }
        window.setInterval(getBleName,1);
        $scope.connectBle=function(){
            $scope.cyd.cydh=$("#cydhCode").val();
            hour=$filter('date')(new Date(),'HH:mm:ss');
            today=$filter('date')($scope.cyd.cyrq,'yyyy.MM.dd');
            time=$filter('date')($scope.cyd.cyrq+" "+hour,'yyyy-MM-dd HH:mm:ss');
            $("#save").attr("disabled", true);
            $("#print").attr("disabled", true);
            $scope.ddzdh;
            var ddz_id=$("#ddz option:selected").val();
            $timeout(function(){
                AddCydFactory.getDdzPhone1(sid,user_id,ddz_id,yh_id);
                $scope.$on('getPhone1.ddz',function(){
                    var result=AddCydFactory.getDdzPhoneResult1();
                    if(result.ACTION_RETURN_CODE=='000000'){
                        $scope.ddzdh=result.ACTION_INFO.RESULT_LIST[0].lxdh;
                    }
                });
            },160);
            if( $scope.tbzt.isCk==true){
                $scope.cyd.tbzt='1';
            }else {
                $scope.cyd.tbzt='';
            }
            if( $scope.yfdfzt.isCk==true){
              $scope.cyd.yfdfzt='2';
            }else {
              $scope.cyd.yfdfzt='1';
            }
            if( $scope.dskdfzt.isCk==true){
              $scope.cyd.dskdfzt='2';
            }else {
              $scope.cyd.dskdfzt='1';
            }
            var ddz=$("#ddz option:selected").val();
            var ddzStr=$("#ddz option:selected").text();
            var shType=$("#shType option:selected").val();

            $scope.cyd.shbm_id=ddz;
            $scope.cyd.shbmmc=ddzStr;
            $scope.cyd.shfs = shType;

            if($scope.cyd.sfszjs=='1'&&$scope.cyd.cydh!=""){
                $scope.cyd.cydh=$scope.cyd.cydh+"-"+$scope.cyd.sl;
            }
            if($scope.dy.sfdyhwbt=='1'&&$scope.dy.hwbtdyfs=='1'){
                $scope.dy.hwdysl=$scope.cyd.sl;
                $scope.dy.read=true;
            }else if($scope.dy.sfdyhwbt=='0'){
                $scope.dy.hwdysl=0;
                $scope.dy.read=true;
            }else {
                $scope.dy.read=false;
                $scope.dy.hwdysl='';
            }
            if ($("#yf").val()==''){
              $("#yf").val(0);
            }
            switch ($("#fkType option:selected").text()){
                case '现付':
                    $scope.cyd.yf_xf=$("#yf").val();
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    break;
                case '回付':
                    $scope.cyd.yf_hf=$("#yf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfxf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    break;
                case '提付':
                    $scope.cyd.yf_tf=$("#yf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_yj='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yfxf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    break;
                case '月结':
                    $scope.cyd.yf_yj=$("#yf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfxf").val("");
                    $("#yfdk").val("");
                    break;
                case '代扣':
                    $scope.cyd.yf_ycdk=$("#yf").val();
                    $scope.cyd.yf_xf='';
                    $scope.cyd.yf_hf='';
                    $scope.cyd.yf_tf='';
                    $scope.cyd.yf_yj='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfxf").val("");
                    break;
                case '多笔付':
                    break;
            }
            //判断承运类型
            if($scope.cyd.shbmmc!=''&&$scope.cyd.mdd==''){
                $scope.cyd.cylx='1';
            }else if($scope.cyd.shbmmc!=''&&$scope.cyd.mdd!=''){
                $scope.cyd.cylx='2';
            }else if($scope.cyd.shbmmc==''&&$scope.cyd.mdd!=''){
                $scope.cyd.cylx='3';
            }
            //判断是否回单
            if($scope.cyd.qdfs!=''&&$scope.cyd.qdfs!=0){
                $scope.cyd.sfyqd=1;
            }else {
                $scope.cyd.sfyqd=2;
            }
            //判断输入限制
            if($scope.cyd.cydh==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入承运单号",
                    duration: 1500
                })
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#cydhCode").focus();
                },160);
            }
            else if($scope.cyd.sl==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入件数",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#jian").focus();
                },160);
            }else if($scope.cyd.yf>3000000|| $scope.cyd.yf_xf>3000000|| $scope.cyd.yf_tf>3000000|| $scope.cyd.yf_hf>3000000 || $scope.cyd.yf_yj>3000000|| $scope.cyd.yf_ycdk>3000000|| $scope.cyd.dsk_hf>3000000|| $scope.cyd.xxf_xf>3000000|| $scope.cyd.xxf_qf>3000000|| $scope.cyd.shf>3000000|| $scope.cyd.thf>3000000|| $scope.cyd.be>3000000|| $scope.cyd.bf>3000000|| $scope.cyd.yf_df>3000000|| $scope.cyd.dsk_xf>3000000|| $scope.cyd.zl>3000000|| $scope.cyd.tj>3000000|| $scope.cyd.qdfs.toString().length>2|| $scope.cyd.sl.toString().length>7){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合理的费用或数量、体积",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
            }
            else if(ddzStr==""&&$scope.cyd.mdd==""){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择到达站或目的地", duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#ddz").focus();
                },160);
            }
            else if(isConeccted=='0'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请先连接打印机", duration: 1500
                });
                $state.go('addPrinter');
                $timeout(function(){
                    Storage.set('cyd',$scope.cyd);
                    Storage.set('fkfs',$("#fkType option:selected").val());
                    Storage.set('shfs',$("#shType option:selected").val());
                },160);
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
            }
            else if($scope.cyd.fhrdh==$scope.cyd.shrdh&&($scope.cyd.fhrdh!=''||$scope.cyd.shrdh!='')){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "收发货电话相同",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#shrdhval").focus();
                },160);
            }else if(!mobile.test($scope.cyd.shrdh)&&!phone.test($scope.cyd.shrdh)&&$scope.cyd.shrdh!=''&&$('#shrdhval').val()!=''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合法电话",
                    duration: 600
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#shrdhval").focus();
                },160);
            }else if(!mobile.test($scope.cyd.fhrdh)&&!phone.test($scope.cyd.fhrdh)&&$scope.cyd.fhrdh!=''&&$('#fhrdhval').val()!=''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入合法电话",
                    duration: 600
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
                $timeout(function(){
                    $("#fhrdhval").focus();
                },160);
            }else if($scope.yfdfzt.isCk==true&&$scope.cyd.yf_df==''){
              $ionicLoading.show({
                noBackdrop: true,
                template: "请输入运费垫付",
                duration: 600
              });
              $("#save").attr("disabled", false);
              $("#print").attr("disabled", false);
              $timeout(function(){
                $("#yfdf").focus();
              },160);
            }else if($scope.dskdfzt.isCk==true&&$scope.cyd.dsk_xf==''){
              $ionicLoading.show({
                noBackdrop: true,
                template: "请输入代收款垫付",
                duration: 600
              });
              $("#save").attr("disabled", false);
              $("#print").attr("disabled", false);
              $timeout(function(){
                $("#dskxf").focus();
              },160);
            }
            else{
                CydChangeFactory.checkCydhExistYd(sid,user_id,dw_id,yh_id,$scope.cyd.cydh,$scope.cyd.cyd_id);
            }
        };

        $scope.$on('checkCydhYd',function(){
            var result=CydChangeFactory.getCountYd();
            var count=result.ACTION_INFO.RESULT_LIST[0].cn;
            if(result.ACTION_RETURN_CODE=='000000'){
                if(count=='0'){
                    CydChangeFactory.ydsaveAndPrint(dw_id,$scope.cyd,yh_id,sid,user_id,zd_id,$scope.cyd.cyrq,bmmc,zsxm);
                }else {
                    $("#cydhCode").focus();
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "承运单号已存在！",
                        duration: 1500
                    });
                    $("#save").attr("disabled", false);
                    $("#print").attr("disabled", false);
                }
            }
        });
        $scope.$on('YdSaveAndPrintCyd.CydUpdated', function() {
            var result=CydChangeFactory.getSpydResult();
            if(result.ACTION_RETURN_CODE=='000000'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "保存成功！",
                    duration: 500
                });
                Storage.set('scrollChangeWz',null);
                Storage.set('cydData',null);
                Storage.set('fkfs',null);
                Storage.set('shfs',null);
                $timeout(function(){
                    var confirmPopup = $ionicPopup.confirm({
                        title:'打印确认',
                        scope:$scope,
                        template: '<div class="list">' +
                        '<div class="item" ng-if="dy.sfdyfhrcg==1">' +
                        '<div class="row" style="font-size: 16px" >' +
                        '<div class="col-60">发货人存根：</div> ' +
                        '<input type="number" id="fhrcgsl" class="col-25" ng-model="dy.fhrcgsl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                        '<div  class="col-15">份</div>' +
                        '</div>' +
                        '</div>' +
                        '<div class="item" ng-show="dy.sfdywlgscg==1">' +
                        '<div class="row" style="font-size: 16px" >' +
                        '<div class="col-60">物流公司存根：</div>' +
                        '<input type="number" id="wlgscgsl" class="col-25" ng-model="dy.wlgscgsl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                        '<div class="col-15">份</div>' +
                        '</div>' +
                        '</div>'+
                        '<div class="item" style="height: 55px"><div class="row" style="font-size: 16px">' +
                        '<div class="col-60">货物标贴份数：</div>' +
                        '<input type="number" id="hwdysl" class="col-25" ng-model="dy.hwdysl" maxlength="20" ng-disabled="dy.read" style="text-align: right;margin-top: -7px;margin-right: 2px">' +
                        '<div class="col-15">份</div>'+
                        '</div>' +
                        '</div>' +
                        '<div class="item" ng-if="dy.sfdyhwbt==1">' +
                        '<div class="row" style="font-size: 16px">' +
                        '<div class="col-70">货贴打印起始号：</div>' +
                        '<input type="number" class="col-20" ng-model="dy.qsh" maxlength="20"  style="text-align: right;margin-top: -7px;margin-right: 17px">' +
                        '</div>' +
                        '</div>'+
                        '</div>',
                        okText:'确定',
                        cancelText:'取消'
                    });
                    confirmPopup.then(function(res) {
                        if (res) {
                            if($scope.dy.qsh==0){
                                $scope.dy.qsh=1;
                            }
                            switch (dyfs){
                                case '1':
                                    for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                      readyToPrintFhrcg();
                                    }
                                    for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                      readyToPrintGscg();
                                    }
                                    for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++) {
                                        //$scope.dy.qsh=i;
                                        hwbh=padLeft(i, 3);
                                        readyToPrintHwbq(hwbh);
                                    }
                                    break;
                                case '2':
                                    for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                      readyToPrintGscg();
                                    }
                                    for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                        //$scope.dy.qsh=i;
                                        hwbh=padLeft(i, 3);
                                        readyToPrintHwbq(hwbh);
                                    }
                                    break;
                                case '3':
                                    for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                      readyToPrintGscg();
                                    }
                                    break;
                                case '4':
                                    break;
                                case '5':
                                    for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                        //$scope.dy.qsh=i;
                                        hwbh=padLeft(i, 3);
                                        readyToPrintHwbq(hwbh);
                                    }
                                    break;
                                case '6':
                                    for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                      readyToPrintFhrcg();
                                    }
                                    for (var i=0;i<=$scope.dy.wlgscgsl-1;i++){
                                      readyToPrintGscg();
                                    }
                                    break;
                                case '7':
                                    for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                      readyToPrintFhrcg();
                                    }
                                    break;
                                case '8':
                                    for (var i=0;i<=$scope.dy.fhrcgsl-1;i++){
                                      readyToPrintFhrcg();
                                    }
                                    for(var i=$scope.dy.qsh;i<=$scope.dy.hwdysl+$scope.dy.qsh-1;i++){
                                        //$scope.dy.qsh=i;
                                        hwbh=padLeft(i, 3);
                                        readyToPrintHwbq(hwbh);
                                    }
                                    break;
                            }
                            $state.go("cydlist");
                            //$ionicHistory.goBack();
                        } else {
                            $state.go('cydlist');
                            //$ionicHistory.goBack();
                        }
                    });
                },500);
                if($scope.dy.sfdyhwbt=='1'&&$scope.dy.hwbtdyfs!='1'){
                    $timeout(function(){
                        $('#hwdysl').focus();
                    },1000);
                }
            }else{
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "保存失败！",
                    duration: 1500
                });
                $("#save").attr("disabled", false);
                $("#print").attr("disabled", false);
            }
        });
    })

    .controller('CydSearchCtrl',function($scope,$cordovaDatePicker,$state,$filter,$ionicLoading,CydChangeFactory,Storage,$stateParams,$ionicHistory){
        var t = new Date();
        var iToDay=t.getDate();
        var iToMon=t.getMonth();
        var iToYear=t.getFullYear();
        var iTDate=new Date(iToYear,iToMon,(iToDay-30));
        var user_id=Storage.get('user_id');
        var sid=Storage.get('sid');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var dw_id=Storage.get("dw_id");
        CydChangeFactory.queryDdz(user_id,sid,yh_id,dw_id,zd_id);
        $scope.$on('CydChange.changeUpdated',function(){
            $scope.zdChoose=CydChangeFactory.getDdz();
        });
        $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date: iTDate,
                mode: 'date',
                maxDate: new Date($scope.endDate) - 10000
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        iTDate=d;
                        $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
                    });
                }
            });
        };

        var endTime = new Date();
        $scope.endDate=$filter('date')(endTime,'yyyy-MM-dd');
        $scope.showEndDate = function(){
            console.log('EndDate');
            var optionsEnd = {
                date : endTime,
                mode : 'date',
                minDate: new Date($scope.startDate) - 10000
            };
            datePicker.show(optionsEnd,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        endTime=d;
                        $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
                    });
                }
            });

        };
        $scope.cyd={
            'cydh':'',
            'fhrmc':'',
            'fhrdh':'',
            'shrmc':'',
            'shrdh':'',
            'fhrmcdh':'',
            'shrmcdh':''
        };
        if ($stateParams.sflx==1){
            $scope.cyd.shrmc=$stateParams.khmc;
            $scope.cyd.shrdh= $stateParams.lxdh;
            $scope.cyd.shrmcdh=$stateParams.khmc+'/'+$stateParams.lxdh;
        }else if($stateParams.sflx==2){
            $scope.cyd.fhrmc=$stateParams.khmc;
            $scope.cyd.fhrdh= $stateParams.lxdh;
            $scope.cyd.fhrmcdh=$stateParams.khmc+'/'+$stateParams.lxdh;
        }
        $scope.searchCyd = function(cyd){
            var shbm_id=$("#shbm option:selected").val();
            var cyzt=$("#cyzt option:selected").val();
            $state.go('cydlist',{cydh:cyd.cydh,kssj:$scope.startDate,jssj:$scope.endDate,fhrmc:cyd.fhrmc,fhrdh:cyd.fhrdh,shrdh:cyd.shrdh,cyzt:cyzt,shbm:shbm_id});
        };
        //选择联系人
        $scope.chooseLinkman = function (htmlbs,sflx) {
            $state.go("linkman",{htmlbs:htmlbs,sflx:sflx});
        };
        $scope.goBack=function(){
            $ionicHistory.goBack();
        };
    })
    .controller('GPSLocationCtrl',function($scope,$stateParams,GPSFactory,Storage){
        //百度地图定位
        var id=$scope.cyd_id ;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        GPSFactory.getGPS(id,sid,user_id,yh_id,dw_id);
        $scope.$on('GPS.GPSUpdated',function(){
            var GPSLocation = GPSFactory.getResult();
            console.log(GPSLocation);
            if(GPSLocation.clzb_x!=''&&GPSLocation.clzb_y!=''){
                var lat = GPSLocation.clzb_y;
                var long = GPSLocation.clzb_x;
                function initialize() {
                    var map = new BMap.Map("map");
                    var point = new BMap.Point(long, lat);
                    map.addOverlay(marker);
                    map.centerAndZoom(point, 15);
                    var myIcon = new BMap.Icon("img/huoche.png", new BMap.Size(34,32));
                    var marker = new BMap.Marker(point,{icon:myIcon});  // 创建标注
                    map.addOverlay(marker);// 将标注添加到地图中
                    var geoc = new BMap.Geocoder();

                    map.addEventListener("click", function (e) {
                        var pt = e.point;
                        geoc.getLocation(pt, function (rs) {
                            var addComp = rs.addressComponents;
                            var opts = {
                                width: 200,     // 信息窗口宽度
                                height: 60,     // 信息窗口高度
                            }
                            var infoWindow = new BMap.InfoWindow("地址：" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber, opts);  // 创建信息窗口对象
                            marker.addEventListener("click", function () {
                                map.openInfoWindow(infoWindow, point); //开启信息窗口
                            });
                        });
                    });
                }

                $(function () {
                    initialize();
                })
            }else {
                $("#map").text("无定位信息");
                $("#map").css({"text-align":"center","font-size":"17px","margin-top":"44px"});
            }

        });
    })
